The Answer by Filotto is correct, and should be accepted.
That code is trying to clear the time-of-day to 00:00:00
. But it is an abuse of the classes, ignores crucial issue of time zone, and uses the notoriously troublesome old date-time classes. Those old classes are now legacy, supplanted by the java.time classes.
The programmer probably wanted either:
- Date-only value
- First moment of the day
LocalDate
The LocalDate
class represents a date-only value without time-of-day and without time zone.
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
ZoneId z = ZoneId.of( “America/Montreal” );
LocalDate today = LocalDate.now( z );
today.toString(): 2016-01-23
ZonedDateTime
For a date-time value start by getting the current date as a LocalDate
object as seen above. Then call atStartOfDay
to get the first moment of the day. Do not assume that first moment is the time 00:00:00
. Anomalies such as Daylight Saving Time (DST) means the first moment may be something like 01:00:00
. So let java.time determine that time-of-day.
ZonedDateTime todayStart = today.atStartOfDay( z );
today.toString(): 2016-01-23T00:00:00-05:00[America/Montreal]
Tip: Never try to determine the last moment of the day. Always use the first moment of the following day. Search Stack Overflow for "Half-Open" for more info.
Instant
Much of your work should be done in UTC. To move your ZonedDateTime
into UTC, extract an Instant
. The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).
Instant instant = zdtStart.toInstant();
Conversion
You should avoid the legacy date-time classes. But if you must interoperate with some old code not yet updated to the java.time types, you can convert to/from java.time. Look for new methods added to the old classes.
java.util.GregorianCalendar gc = GregorianCalendar.from( zdtStart );
java.util.Date d = Date.from( instant );
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).
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.