TL; dr
The Joda-Time project is in maintenance mode, now superseded by java.time classes.
- Just use the
java.time.Instant class. - Not necessary:
LocalDateTimejava.sql.Timestamp- Strings
Capture the current moment in UTC.
Instant.now()
To save this point in the database:
myPreparedStatement.setObject( β¦ , Instant.now() )
To extract this point from the database:
myResultSet.getObject( β¦ , Instant.class )
To adjust the time of the wall clock in relation to a specific time zone.
instant.atZone( z )
LocalDateTime - invalid class
Other answers are correct, but they do not indicate that LocalDateTime is the wrong class for your purpose.
In both java.time and Joda-Time, LocalDateTime intentionally devoid of any concept of time zone or offset from UTC. Thus, this does not represent a moment and is not a point on the timeline. LocalDateTime is an approximation of potential moments in the range of about 26-27 hours.
Use LocalDateTime for either when the zone / offset is unknown (not a good situation), or when the zone uncertainty is uncertain. For example, βChristmas begins at the first moment of December 25, 2018β will be presented as LocalDateTime .
Use ZonedDateTime to represent a moment in a specific time zone. For example, Christmas starting in any particular zone, such as Pacific/Auckland or America/Montreal , will be represented by a ZonedDateTime object.
For a moment, always in UTC use Instant .
Instant instant = Instant.now() ;
Timezone application. The same moment, the same point on the timeline, but viewed with a different wall clock time.
ZoneId z = ZoneId.of( "Africa/Tunis" ) ; ZonedDateTime zdt = instant.atZone( z ) ; // Same moment, different wall-clock time.
So, if I can just convert between LocalDate and LocalDateTime,
No, the wrong strategy. If you only have a date value and you want to enter a date-time, you must specify the time of day. This time of day may not be valid on this date for a specific zone - in this case, the ZonedDateTime class automatically adjusts the time of day as necessary.
LocalDate ld = LocalDate.of( 2018 , Month.JANUARY , 23 ) ; LocalTime lt = LocalTime.of( 14 , 0 ) ; // 14:00 = 2 PM. ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;
If you want the first moment of the day to be your time of day, let java.time determine that moment. Do not think that the day starts at 00:00:00. Anomalies, such as daylight saving time (DST), mean that the day can start at another time, for example, 01:00:00.
ZonedDateTime zdt = ld.atStartOfDay( z ) ;
java.sql.Timestamp is the wrong class
java.sql.Timestamp is part of the problematic old time classes, which are now obsolete, are completely superseded by the java.time classes. This class was used to represent the moment in UTC with a resolution of nanoseconds . This target is now used with java.time.Instant .
JDBC 4.2 with getObject / setObject
Starting with JDBC 4.2 and later, your JDBC driver can directly exchange java.time objects with the database by calling:
For example:
myPreparedStatement.setObject( β¦ , instant ) ;
β¦ as well as β¦
Instant instant = myResultSet.getObject( β¦ , Instant.class ) ;
Modern Heritage Transformation
If you must interact with old code that has not yet been updated to java.time, convert back and forth using the new methods added to the old classes.
Instant instant = myJavaSqlTimestamp.toInstant() ; // Going from legacy class to modern class.
β¦as well asβ¦
java.sql.Timestamp myJavaSqlTimestamp = java.sql.Timestamp.from( instant ) ; // Going from modern class to legacy class.
About java.time
The java.time framework is built into Java 8 and later. These classes supersede the nasty old obsolete time classes such as java.util.Date , Calendar and SimpleDateFormat .
The Joda-Time project, now in maintenance mode , advises switching to the java.time classes.
To learn more, check out the Oracle tutorial . And search for qaru for many examples and explanations. The specification is JSR 310 .
You can exchange java.time objects directly with your database. Use a JDBC driver compatible with JDBC 4.2 or later. No strings needed, no java.sql.* Needed.
Where can I get java.time classes?
- Java SE 8 , Java SE 9 and later
- Built in.
- Part of the standard Java API with integrated implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Most of the functionality of java.time is included back in Java 6 and 7 in ThreeTen-Backport .
- Android
- Later versions of the Android package implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts the ThreeTen-Backport (mentioned above). See How to use ThreeTenABP ....
The ThreeTen-Extra project extends java.time with additional classes. This project is a proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and others .