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.
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
Calendar
class. So I can safely assume (a) they are open to an alternative, new classes built into Java, and (b) they are simply operating in ignorance, not yet having been informed of the java.tine classes available to them.
Commented
Jul 25, 2017 at 16:34
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);
Date
. So ideally one would stop once the Instant
object was obtained. Only convert to Date
if absolutely necessary such as interacting with some older code not yet updated to java.time.
Commented
Mar 29, 2017 at 7:29
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();
}
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();
toDate
), and back again. So you can make use of Joda-Time for calculations without disturbing other existing code.Date
. That class is poorly designed and long outdated. Instead useLocalDate
from java.time, the modern Java date and time API. And other classes from the same API depending on your precise requirements.