tl;dr
LocalDateTime.now(
ZoneId.of( "America/Montreal" )
)
.toString()
2018-01-23T12:34:56.123456
If you want to drop the minutes and seconds, truncate.
LocalDateTime.now(
ZoneId.of( "America/Montreal" )
)
.truncatedTo( ChronoUnit.HOURS )
.toString()
2018-01-23T12:00:00
Details
The Calendar
class is now legacy, supplanted by the modern java.time classes. Also, Calendar
always carries a time zone, so cannot meet your needs.
In java.time, the three classes named with “Local…” purposely lack any concept of time zone or offset-from-UTC. The word local means any locality or every locality, not a particular locality.
A date-only value, lacking time-of-day and lacking time zone.
A time-of-day value, lacking a date and lacking a time zone. Limited to a single 24-hour period.
A date with time-of-day, lacking time zone.
Does not represent a moment, is not a point on the timeline. Represents potential moments along a range of about 26-27 hours (the various offsets used by various time zones). A LocalDateTime
has no real meaning until you apply a time zone or offset-from-UTC to determine a specific moment.
Current moment
Capture the current moment in UTC by using the Instant
class. This class uses a resolution of nanoseconds, but the current moment may not be so fine, depending on the limitations of your JVM implementation, host OS, and host computer hardware clock.
Instant instant = Instant.now() ; // Capture current moment in UTC.
If you want to view this current moment thorough the lens of the wall-click time used by the people of a particular region, apply a time zone. Apply a ZoneId
to get a ZonedDateTime
.
ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
To use just the date and time-of-day from that ZonedDateTime
, but discard the time zone, produce a LocalDateTime
object.
LocalDateTime ldt = zdt.toLocalDateTime() ; // Extract the date and time-of-day but omit the time zone.
As a shortcut you can skip the zoned objects, the Instant
and the ZonedDateTime
. You still need a time zone, a ZoneId
, as for any given the moment the date and time-of-day both vary around the globe by zone. The ZoneId
is used in determining the current moment as seen in the wall-clock time used by the people of that region. Once captured, the ZoneId
is then forgotten by the resulting LocalDateTime
object. If you wanted to keep that zone, remember that zone, you’d be using ZonedDateTime
instead of LocalDateTime
.
LocalDateTime ldt = LocalDateTime.now( z ) ;
If you want the JVM’s current default time zone at runtime, pass a ZoneId
returned by a call to ZoneId.systemDefault
. Beware that the default can be changed at any moment by any code in any thread of any app within that JVM.
If you want UTC rather than some time zone, pass the constant from the ZoneOffset
subclass of ZoneId
, ZoneOffset.UTC
.
LocalDateTime.now( ZoneOffset.UTC ) // Capture the current date and time of day as seen in UTC (an offset of zero), and then discard that offset/zone info.
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.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
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.