问题
I have a variable of the type java.util.Date.
How can I set the time part to 00:00:00?
I am not allowed to use an Apache Commons library or JodaTime. The java.util.Calendar is probably my only option.
回答1:
To remove time from the Date object completely, you could use this:
public static Date removeTime(Date 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);
return cal.getTime();
}
Pass into the method the Date object that you want to modify and it will return a Date that has no hours/minutes etc.
If changing the Date object itself isn't required, use SimpleDateFormat. Set the format the way you want ie. remove hours/minutes. Then call the format method and pass in the Date object you want changed.
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy");
System.out.println(sdf.format(yourDate));
回答2:
tl;dr
If you meant midnight in UTC in an old legacy java.util.Date object…
java.util.Date.from(
OffsetDateTime.of(
LocalDate.of( 2017 , Month.JANUARY , 23 ) ,
LocalTime.MIN ,
ZoneOffset.UTC
).toInstant()
)
If possible, discard the legacy Date portion above and just use the modern java.time.OffsetDateTime or java.time.Instant object…
OffsetDateTime.of(
LocalDate.of( 2017 , Month.JANUARY , 23 ) ,
LocalTime.MIN ,
ZoneOffset.UTC
).toInstant()
If you meant midnight in a particular time zone rather than UTC…
LocalDate.now()
.atStartOfDay()
Better to specify your desired/expected time zone than rely implicitly on current default.
LocalDate.now( ZoneId.of( "Pacific/Auckland" ) )
.atStartOfDay( ZoneId.of( "Pacific/Auckland" ) )
Details
Caution: Not every day in every time zone has a time of 00:00:00. Daylight Savings Time can jump to 1 AM, eliminating a midnight.
java.time
Using java.time classes that supplant the troublesome old legacy date-time classes such as java.util.Date and .Calendar.
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate localDate = LocalDate.now( z );
ZonedDateTime zdt = localDate.atStartOfDay( z );
Convert to/from legacy classes
You should stick to the modern java.time classes whenever possible. But if you must have have instances of the old legacy classes, you can convert to/from java.time. Look to new methods added to the old classes.
Calendar myLegacyCal = GregorianCalendar.from( zdt ) ;
Date myLegacyDate = Date.from( zdt.toInstant() ) ;
If you want a java.util.Date, which is always in UTC time zone, to have a time-of-day of 00:00:00, use OffsetDateTime with the constant ZoneOffset.UTC.
OffsetDateTime odt = OffsetDateTime.of( localDate , LocalTime.MIN , ZoneOffset.UTC ) ;
Date myLegacyDate = Date.from( odt.toInstant() ) ;
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.
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
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
- 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.
Joda-Time
UPDATE: The Joda-Time project is now in maintenance mode. The team advises migration to the java.time classes. This section left intact as history.
Use Joda-Time 2.3, with a method for this very purpose: withTimeAtStartOfDay().
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
DateTime now = new DateTime();
DateTime firstMomentOfToday = now.withTimeAtStartOfDay();
System.out.println( "now: " + now );
System.out.println( "firstMomentOfToday: " + firstMomentOfToday );
When run…
now: 2013-12-05T23:00:23.785-08:00
firstMomentOfToday: 2013-12-05T00:00:00.000-08:00
回答3:
In Java 8 'java.time.LocalDate.atStartOfDay()' can be used too:
Date date = new Date();
Instant inst = date.toInstant();
LocalDate localDate = inst.atZone(ZoneId.systemDefault()).toLocalDate();
Instant dayInst = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant();
Date day = Date.from(dayInst);
回答4:
Like @Sionnach733, to remove time from Date, you can write this function for your DateTimeUtils with clear:
public static Date getDateOnly(final Date input) {
final Calendar cal = Calendar.getInstance();
cal.setTime(input);
cal.clear(Calendar.HOUR_OF_DAY);
cal.clear(Calendar.MINUTE);
cal.clear(Calendar.SECOND);
cal.clear(Calendar.MILLISECOND);
return cal.getTime();
}
回答5:
Latest version courtesy of Ole.V.V
import java.time.*;
Instant dInst = LocalDate.of(1980, Month.JANUARY, 8).atStartOfDay(ZoneOffset.UTC).toInstant();
long milli = dInst.toEpochMilli();
来源:https://stackoverflow.com/questions/20414343/how-to-set-time-of-java-util-date-instance-to-000000