We are using org.thirteen.bp
library to manage different format with timezone information. I have parsed the string value to LocalDateTime with its format but is there any way to convert org.thirteen.bp.LocalDateTime
to java.sql.Timestamp
?

- 3,528
- 7
- 35
- 50
-
2It's not `org.thirteen` but `org.threeten` (three-ten) which refers to [JSR-310](https://community.oracle.com/docs/DOC-983209), the new date and time API that was added to Java 8. This library is a backport of the new API so that you can use it in Java versions older than Java 8. See https://www.threeten.org/ – Jesper May 16 '19 at 14:10
2 Answers
is there any way to convert org.thirteen.bp.LocalDateTime to java.sql.Timestamp?
Yes: DateTimeUtils.toSqlTimestamp
The DateTimeUtils
class provides a conversion method.
LocalDateTime ldt = LocalDateTime.parse( "2019-01-23T01:23:45" ) ;
java.sql.Timestamp ts = org.threeten.bp.DateTimeUtils.toSqlTimestamp( ldt ) ;
…but…
You should know that storing a LocalDateTime
object by using a java.sql.Timestamp
is a terrible hack.
The LocalDateTime
cannot represent a moment, but Timestamp
does.
The problem is that before the java.time classes and JSR 310 came along, Java had no way to represent a date and time-of-day without a time zone or offset-from-UTC. So there was no class back then equivalent to the SQL-standard type of TIMESTAMP WITHOUT TIME ZONE
. They faked it by using Timestamp
. This is one of the many terrible design decisions made by members of the early Java team who clearly did not understand the issues involved in date-time handling.
For more explanation, see this Answer by Ole V.V. on a similar Question.
When you eventually move to Java 8 or later, and can transition from ThreeTen-Backport to using the built-in java.time classes, you can avoid entirely the use of java.sql.Timestamp
.
LocalDateTime ldt = LocalDateTime.parse( "2019-01-23T01:23:45" ) ;
myPreparedStatement.setObject( … , ldt ) ; // Appropriately storing a date and time-of-day without zone/offset into a database column of type TIMESTAMP WITHOUT TIME ZONE.
Retrieval.
LocalDateTime ldt = myResultSet.getObject( … , LocalDateTime.class ) ;
Presumably, you are using are using ThreeTen-Backport because you are still using Java 6 or Java 7. That means you do not have JDBC 4.2 or later. So you must use the hack seen at the top of this Answer.

- 303,325
- 100
- 852
- 1,154
Looks like the library you mentioned is fork of Java 8 Date and Time API.
You should be able to get the java.sql.Timestamp
using below code
new java.sql.Timestamp(LocalDateTime.getLong(ChronoField.INSTANT_SECONDS))

- 3,865
- 18
- 23
-
For this format "yyyyMMdd'T'HHmmss.SSSSSSZZ" i get 1970 date always. It is not giving me expected date – syv May 15 '19 at 21:33
-
1This Answer buys into the false premise that a `LocalDateTime` is the same kind of information as a `Timestamp`. They are not the same at all. – Basil Bourque May 15 '19 at 23:29
-
-
Now I get `org.threeten.bp.temporal.UnsupportedTemporalTypeException: Unsupported field: InstantSeconds` (this is related to @BasilBourque’s comment). – Ole V.V. May 17 '19 at 13:23