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 recommended way to do date/time manipulation is to use a Calendar
object:
Calendar cal = Calendar.getInstance(); // locale-specific
cal.setTime(dateObject);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
long time = cal.getTimeInMillis();
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.
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 a ChronoUnit
(or a TemporalUnit
) smaller than DAYS to execute the truncation (as the truncation is applied only to the time part of LocalDateTime, not to the date part).
Have you looked at Joda ? It's a much easier and more intuitive way to work with dates and times. For instance you can convert trivially between (say) LocalDateTime and LocalDate objects.
e.g. (to illustrate the API)
LocalDate date = new LocalDateTime(milliseconds).toLocalDate()
Additionally it solves some thread-safety issues with date/time formatters and is to be strongly recommended for working with any date/time issues in Java.
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
date = cal.getTime();
With Joda you can easily get the expected date.
As of version 2.7 (maybe since some previous version greater than 2.2), as a commenter notes, toDateMidnight
has been deprecated in favor or the aptly named withTimeAtStartOfDay()
, making the convenient
DateTime.now().withTimeAtStartOfDay()
possible.
Benefit added of a way nicer API.
With older versions, you can do
new DateTime(new Date()).toDateMidnight().toDate()
I did the truncation with new java8 API. I faced up with one strange thing but in general it's truncate...
Instant instant = date.toInstant();
instant = instant.truncatedTo(ChronoUnit.DAYS);
date = Date.from(instant);
tl;dr
LocalDateTime.parse( // Lacking an offset or time zone, parse as a `LocalDateTime`. *Not* a specific moment in time.
"2008-01-01 13:15:00".replace( " " , "T" ) // Alter input string to comply with ISO 8601 standard format.
)
.toLocalDate() // Extract a date-only value.
.atStartOfDay( // Do not assume the day starts at 00:00:00. Let class determine start-of-day.
ZoneId.of( "Europe/Paris" ) // Determining a specific start-of-day requires a time zone.
) // Result is a `ZonedDateTime` object. At this point we have a specific moment in time, a point on the timeline.
.toString() // Generate a String in standard ISO 8601 format, wisely extended to append the name of the time zone in square brackets.
2008-01-01T00:00+01:00[Europe/Paris]
To generate a String in your desired format, pass a DateTimeFormatter
.
LocalDateTime.parse( // Lacking an offset or time zone, parse as a `LocalDateTime`. *Not* a specific moment in time.
"2008-01-01 13:15:00".replace( " " , "T" ) // Alter input string to comply with ISO 8601 standard format.
)
.toLocalDate() // Extract a date-only value.
.atStartOfDay( // Do not assume the day starts at 00:00:00. Let class determine start-of-day.
ZoneId.of( "Europe/Paris" ) // Determining a specific start-of-day requires a time zone.
) // Result is a `ZonedDateTime` object. At this point we have a specific moment in time, a point on the timeline.
.format( // Generate a String representing the object’s value.
DateTimeFormatter.ISO_LOCAL_DATE_TIME // Built-in predefined formatter close to what you want.
)
.replace( "T" , " " ) // Replace the standard’s use of a 'T' in the middle with your desired SPACE character.
2008-01-01 00:00:00
Details
Other Answers are correct, but use old date-time classes now outmoded by the java.time framework.
java.time
The java.time framework is built into Java 8 and later. Much of the java.time functionality is back-ported to Java 6 & 7 (ThreeTen-Backport) and further adapted to Android (ThreeTenABP).
First alter the input string to comply with the canonical version of ISO 8601 format. The standard ISO 8601 formats are used by default in java.time classes for parsing/generating strings that represent date-time values. We need to replace that SPACE in the middle with a T
.
String input = "2008-01-01 13:15:00".replace( " " , "T" ); // → 2008-01-01T13:15:00
Now we can parse it as a LocalDateTime
, where “Local” means no specific locality. The input lacks any offset-from-UTC or time zone info.
LocalDateTime ldt = LocalDateTime.parse( input );
ldt.toString()… 2008-01-01T13:15:00
If you do not care about time-of-day nor time zone, then convert to a LocalDate
.
LocalDate ld = ldt.toLocalDate();
ld.toString()… 2008-01-01
First Moment Of Day
If instead you want the time-of-day set to the first moment of the day, use a ZonedDateTime
class, then convert to a LocalDate
object to call its atStartOfDay
method. Be aware that the first moment may not be the time 00:00:00
because of Daylight Saving Time or perhaps other anomalies.
The time zone is crucial because for any given moment the date varies around the world by zone. For example, a few moments after midnight in Paris is a new day for Parisians but is still “yesterday” in Montréal for the Canadians.
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ldt.atZone( zoneId );
LocalDate ldFromZdt = zdt.toLocalDate();
ZonedDateTime zdtStartOfDay = ldFromZdt.atStartOfDay( zoneId );
zdtStartOfDay.toString()… 2008-01-01T00:00:00-05:00[America/Montreal]
UTC
To see that moment through the lens of the UTC time zone, extract a Instant
object. Both the ZonedDateTime
and Instant
will represent the same moment on the timeline but appear as two different wall-clock times.
An Instant
is the basic building-block class in java.time, always in UTC by definition. Use this class frequently, as you should generally be doing your business logic, data storage, and data exchange in UTC.
Instant instant = zdtStartOfDay.toInstant();
instant.toString()… 2008-01-01T05:00:00Z
We see 5 AM rather than stroke-of-midnight. In standard format, the Z
on the end is short for Zulu
and means “UTC”.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
- Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
Use DateUtils from Apache, with truncate, like this:
DateUtils.truncate(Calendar.getInstance().getTime(), Calendar.DATE);
For timestamps:
timestamp -= timestamp % (24 * 60 * 60 * 1000)
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
Just clear()
redundant fields.
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.clear(Calendar.MINUTE);
calendar.clear(Calendar.SECOND);
calendar.clear(Calendar.MILLISECOND);
Date truncatedDate = calendar.getTime();
From Java 8 a better option is to use truncatedTo
method of LocalDateTime
, e.g.:
LocalDateTime.now().truncatedTo(ChronoUnit.DAYS)
Use the Calendar class's set()
method to set the HOUR_OF_DAY, MINUTE, SECOND
and MILLISECOND
fields to zero.
You can do this to avoid timezone issue:
public static Date truncDate(Date date) {
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
cal.setTime(date);
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();
}
Although Java Date
object is timestamp value, but during truncate, it will be converted to local timezone, so you will get surprising value if you expect value from UTC 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 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.
It really annoyed me that the new "improved" calendar constructor doesn't take an int for milliseconds like the "awful" old Date one. I then got really cross and wrote this:
long stuffYou = startTime.getTimeInMillis() % 1000;
startTime.setTimeInMillis(startTime.getTimeInMillis() - stuffYou);
I didn't use the word "stuff" at the time, but then I discovered the happiness of this:
startTime.set(Calendar.MILLISECOND, 0);
But I'm still quite cross about it.
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();
}
Might be a late response but here is a way to do it in one line without using any libraries:
new SimpleDateFormat("yyyy-MM-dd").parse(new SimpleDateFormat("yyyy-MM-dd").format(YOUR_TIMESTAMP))
With Joda-Time since version 2.0 you can use LocalDate.toDate()
.
Simply
// someDatetime is whatever java.util.Date you have.
Date someDay = new LocalDate(someDatetime).toDate();
For all the answers using Calendar, you should use it like this instead
public static Date truncateDate(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, c.getActualMinimum(Calendar.HOUR_OF_DAY));
c.set(Calendar.MINUTE, c.getActualMinimum(Calendar.MINUTE));
c.set(Calendar.SECOND, c.getActualMinimum(Calendar.SECOND));
c.set(Calendar.MILLISECOND, c.getActualMinimum(Calendar.MILLISECOND));
return c.getTime();
}
But I prefer this:
public static Date truncateDate(Date date) {
return new java.sql.Date(date.getTime());
}
精彩评论