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.
set(Calendar. MINUTE, 0); calendar.
You can make use of the following DateFormat. SimpleDateFormat myDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); myDate. setTimeZone(TimeZone. getTimeZone("UTC")); Date newDate = myDate.
No time data is kept. In fact, the date is stored as milliseconds since the 1st of January 1970 00:00:00 GMT and the time part is normalized, i.e. set to zero. Basically, it's a wrapper around java. util.
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));
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" ) )
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.
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 );
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() ) ;
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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With