0

I'm trying to convert 2020-11-03T14:03:45.173649-05:00 into timestamp using java.

I tried using Timestamp.valueOf("2020-11-03T14:03:45.173649-05:00"); here but getting error saying java.lang.IllegalArgumentException:Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]. Is there any way to convert Date time having timezone offset to timestamp?

  • What makes you think that you need an old-fashioned `java.sql.Timestamp`? Since JDBC 4.2 you can parse your string into an `OffsetDateTime` which you can pass directly to `PreparedStatement` and other JDBC classes. The story is longer than I have told it here, but basically you should use java.tiem, the modern Java date and time API and not `Timestamp`. – Ole V.V. Nov 04 '20 at 17:30

1 Answers1

1

Since this is an ISO format, the easiest would be to get an OffsetDateTime first and then convert to Timestamp:

var input = "2020-11-03T14:03:45.173649-05:00";
var odt = OffsetDateTime.parse(input);
var ts = Timestamp.from(odt.toInstant());

Alternatively, you can parse the string using a SimpleDateFormat.

assylias
  • 321,522
  • 82
  • 660
  • 783