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.
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 itswithTimeAtStartOfDay
method. Be aware that the first moment may not be the time00:00:00
because of Daylight Saving Time or perhaps other anomalies.From java.util.Date JavaDocs:
and from the java.sql.Date JavaDocs:
So, the best approach is to use java.sql.Date if you are not in need of the time part
and the output is:
Have you looked at the DateUtils truncate method in Apache Commons Lang?
will remove the time element.
I fixed the issue like this(in Eastern eight zone(Beijing time)):
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:
And
prints diferent time info in different time zones: If you set your pc time zone as UTC, you get
But if you set the time zone as (UTC +8:00) Beijing, Chongqing, HongKong, Urumq, you get:
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.
}
Just a quick update in light of the java.time classes now built into Java 8 and later.
LocalDateTime
has atruncatedTo
method that effectively addresses what you are talking about here:This will express the current time down to minutes only:
You may use any
ChronoUnit
(or indeed anyTemporalUnit
) to execute the truncation.Use DateUtils from Apache, with truncate, like this: