Java Date cut off time information

2019-01-05 00:31发布

I have a Java Date object containing date and time information. I want to write a method that cuts off the time information, truncates the hours-minutes-seconds, so I only have the date left.

Example input:

2008-01-01 13:15:00

Expected output:

2008-01-01 00:00:00

Do you have a tip? I tried doing something like this:

(timestamp / (24 * 60 * 60 * 1000)) * (24 * 60 * 60 * 1000)

but I ran into problems with the timezone.

20条回答
甜甜的少女心
2楼-- · 2019-01-05 01:11

The question is contradictory. It asks for a date without a time of day yet displays an example with a time of 00:00:00.

Joda-Time

UPDATE: The Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes. See my other Answer for java.time solution.

If instead you want the time-of-day set to the first moment of the day, use a DateTime object on the Joda-Time library and call its withTimeAtStartOfDay method. Be aware that the first moment may not be the time 00:00:00 because of Daylight Saving Time or perhaps other anomalies.

查看更多
Luminary・发光体
3楼-- · 2019-01-05 01:12

From java.util.Date JavaDocs:

The class Date represents a specific instant in time, with millisecond precision

and from the java.sql.Date JavaDocs:

To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.

So, the best approach is to use java.sql.Date if you are not in need of the time part

java.util.Date utilDate = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis());

and the output is:

java.util.Date : Thu Apr 26 16:22:53 PST 2012
java.sql.Date  : 2012-04-26
查看更多
老娘就宠你
4楼-- · 2019-01-05 01:14

Have you looked at the DateUtils truncate method in Apache Commons Lang?

Date truncatedDate = DateUtils.truncate(new Date(), Calendar.DATE);

will remove the time element.

查看更多
We Are One
5楼-- · 2019-01-05 01:16

I fixed the issue like this(in Eastern eight zone(Beijing time)):

private Date getTruncatedDate(Date d) {
    if (d == null) {
        return null;
    }
    long h = 60 * 60 * 1000L;
    long dateStamp = d.getTime() - (d.getTime() + 8 * h) % (24 * h);
    return new Date(dateStamp);
}

First of all, you should be clear what is time stamp. Time stamp is the total milliseconds from Jan 01 00:00:00 1970 of GMT(same as UTC), or Thu Jan 01 08:00:00 CST 1970 to now.

Remember: Time stamp is independent of time zone.

So you get same result with the following statement in differnt time zones:

System.out.println(new Date().getTime());

And

System.out.println(new Date(0));

prints diferent time info in different time zones: If you set your pc time zone as UTC, you get

Thu Jan 01 00:00:00 UTC 1970

But if you set the time zone as (UTC +8:00) Beijing, Chongqing, HongKong, Urumq, you get:

Thu Jan 01 08:00:00 CST 1970

Java gets the time stamp, then displays date and time info according on the time zone.

For the introduction of how Java displays date and time info in different time zones, how to trancate the time info is easy. You should get the time stamp , and take the time zone into account when cut off the time info. Then you can create a new Date object with the cut time stamp(we can call it date stamp), java will compensate the time zone when displays date info.

As in Eastern eight zone(Beijing time), the Beijing time is earlier 8 hours than GMT, so you should subtract more 8 hours when you do the modulo operation. That's to say, you should get the GMT time first, then Java will add 8 hours when display time based on your pc's time zone setting.


The time zone issue is obscure, and also puzzles me for a long time. Now I make it clear. Hope helps.


2018-01-04 The method below also works.

private Date getTruncatedDate2(Date d) {
    Calendar cal = Calendar.getInstance(); // locale-specific
    cal.setTime(d);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);


return cal.getTime();

}

查看更多
贪生不怕死
6楼-- · 2019-01-05 01:21

Just a quick update in light of the java.time classes now built into Java 8 and later.

LocalDateTime has a truncatedTo method that effectively addresses what you are talking about here:

LocalDateTime.now().truncatedTo(ChronoUnit.MINUTES)

This will express the current time down to minutes only:

2015-03-05T11:47

You may use any ChronoUnit (or indeed any TemporalUnit) to execute the truncation.

查看更多
甜甜的少女心
7楼-- · 2019-01-05 01:21

Use DateUtils from Apache, with truncate, like this:

DateUtils.truncate(Calendar.getInstance().getTime(), Calendar.DATE);
查看更多
登录 后发表回答