Resetting the time part of a timestamp in Java

后端 未结 10 1213
慢半拍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: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

提交回复
热议问题