Defining ‘Midnight’
The word “midnight” is tricky to define.
Some think of it as the moment before a new day starts. Trying to represent that in software as tricky as the last moment of the day can always be subdivided as a smaller fraction of a second.
I suggest a better way of thinking about this is to get “first moment of the day”.
This supports the commonly used approach of defining a span of time as ‘Half-Open’, where the beginning is inclusive while the ending is exclusive. So a full day starts with the first moment of the day and runs up to, but not including, the first moment of the following day. A full day would like this (notice the date going from the 3rd to the 4th):
2016-02-03T00:00:00.0-08:00[America/Los_Angeles]/2016-02-04T00:00:00.0-08:00[America/Los_Angeles]
Joda-Time
If using the Joda-Time library, call withTimeAtStartOfDay
.
Note how we specify the time zone. If omitted, the JVM’s current default time zone is implicitly applied. Better to be explicit.
DateTime todayStart = DateTime.now( DateTimeZone.forID( "America/Montreal" ) ).withTimeAtStartOfDay() ;
If using Java 8 or later, better to use the java.time package built into Java. See sibling Answer by Jens Hoffman.