Resetting the time part of a timestamp in Java

后端 未结 10 1207
慢半拍i
慢半拍i 2020-12-04 23:56

In Java, given a timestamp, how to reset the time part alone to 00:00:00 so that the timestamp represents the midnight of that particular day ?

In T-SQL, this query

相关标签:
10条回答
  • 2020-12-05 00:24

    Since I don't do much DateTime manipulation, this might not be the best way to do it. I would spawn a Calendar and use the Date as source. Then set hours, minutes and seconds to 0 and convert back to Date. Would be nice to see a better way, though.

    0 讨论(0)
  • 2020-12-05 00:27

    I prefer this solution:

    GregorianCalendar now = new GregorianCalendar();
    GregorianCalendar calendar = new GregorianCalendar(
                  now.get(GregorianCalendar.YEAR), now.get(GregorianCalendar.MONTH), 
                  now.get(GregorianCalendar.DAY_OF_MONTH));
    
    0 讨论(0)
  • 2020-12-05 00:28

    Do this

    import org.apache.commons.lang.time.DateUtils;
    
    Date myDate = new Date();
    System.out.println(myDate);        
    System.out.println(DateUtils.truncate(myDate, Calendar.DATE));
    

    and the output is

    Wed Mar 19 14:16:47 PDT 2014
    Wed Mar 19 00:00:00 PDT 2014

    0 讨论(0)
  • 2020-12-05 00:30

    Assuming your "timestamp" is a java.util.Date, which is represented as the number of milliseconds since the beginning of the epoch (Jan 1, 1970), you can perform the following arithmetic:

    public static Date stripTimePortion(Date timestamp) {
        long msInDay = 1000 * 60 * 60 * 24; // Number of milliseconds in a day
        long msPortion = timestamp.getTime() % msInDay;
        return new Date(timestamp.getTime() - msPortion);
    }
    
    0 讨论(0)
  • 2020-12-05 00:37

    You can go Date->Calendar->set->Date:

    Date date = new Date();                      // timestamp now
    Calendar cal = Calendar.getInstance();       // get calendar instance
    cal.setTime(date);                           // set cal to date
    cal.set(Calendar.HOUR_OF_DAY, 0);            // set hour to midnight
    cal.set(Calendar.MINUTE, 0);                 // set minute in hour
    cal.set(Calendar.SECOND, 0);                 // set second in minute
    cal.set(Calendar.MILLISECOND, 0);            // set millis in second
    Date zeroedDate = cal.getTime();             // actually computes the new Date
    

    I love Java dates.

    Note that if you're using actual java.sql.Timestamps, they have an extra nanos field. Calendar of course, knows nothing of nanos so will blindly ignore it and effectively drop it when creating the zeroedDate at the end, which you could then use to create a new Timetamp object.

    I should also note that Calendar is not thread-safe, so don't go thinking you can make that a static single cal instance called from multiple threads to avoid creating new Calendar instances.

    0 讨论(0)
  • 2020-12-05 00:39

    Just trying to put an answer for Java-8 , though answers using Calendar are valid too but lots of folks are not using that class anymore.

    Refer SO Answers this & this to understand conversions between java.time.ZonedDateTime & java.sql.Timestamp

    then you can simply truncate ZonedDateTime on days & reconvert back to Timestamp

    Timestamp.valueOf(ZonedDateTime.now().truncatedTo(ChronoUnit.DAYS).toLocalDateTime())
    
    0 讨论(0)
提交回复
热议问题