Zulu time
The Z
on the end of your input string means UTC, and is pronounced “Zulu”.
ISO 8601
Your input string is in standard ISO 8601 format. The java.time classes use these standard formats by default when parsing or generating strings.
Instant
Parse your string as an Instant
. Instant represents a moment in UTC with a resolution of nanoseconds.
Instant instant = Instant.parse("2018-09-26T15:05:19.1121042Z") ;
Your JDBC driver may be able to take that Instant
.
myPreparedStatement.setObject( … , instant ) ;
OffsetDateTime
If not, your JDBC 4.2 or later driver is required to accept OffsetDateTime
.
OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;
myPreparedStatement.setObject( … , odt ) ;
Avoid java.sql.Timestamp
If you have on older JDBC driver before JDBC 4.2, then fall back to using the terrible java.sql.Timestamp
. But use these legacy date-time classes only if you absolutely must, as they are an awful mess.
You can convert between the modern and legacy classes by calling new conversion methods added to the old classes.
java.sql.Timestamp ts = java.sql.Timestamp.from( instant ) ;
…and…
Instant instant = ts.toInstant() ;
Time zone
Presumably you were asking about java.sql.Timestamp
because you are exchanging the value with a database.
Your time zone of Chicago is irrelevant to database work, as most databases store a moment in UTC.
ZonedDateTime
But for presentation to the user, you may want to adjust from UTC to a time zone.
ZoneId z = ZoneId.of( "America/Chicago" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
Generate a string in localized format
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( Locale.US ) ;
String output = zdt.format( f ) ;