I want to parse a timestamp, like this - \"2016-03-16 01:14:21.6739\"
. But when I use the SimpleDateFormat
to parse it, I find that it outputs an i
LocalDateTime.parse(
"2016-03-16 01:14:21.6739".replace( " " , "T" ) // Comply with ISO 8601 standard format.
)
As others noted, java.util.Date
has millisecond resolution. That means up to 3 digits of a decimal fraction of second.
You have 4 digits in your input string, one too many. Your input value demands finer resolution such as microseconds or nanoseconds.
Instead of using the flawed, confusing, and troublesome java.util.Date/.Calendar classes, move on to their replacement: the java.time framework built into Java 8 and later.
The java.time classes have a resolution of nanosecond, up to nine digits of decimal fraction of a second. For example:
2016-03-17T05:19:24.123456789Z
Your string input is almost in standard ISO 8601 format used by default in java.time when parsing/generating textual representations of date-time values. Replace that space in the middle with a T
to comply with ISO 8601.
String input = "2016-03-16 01:14:21.6739".replace( " " , "T" );
A LocalDateTime is an approximation of a date-time, without any time zone context. Not a moment on the timeline.
LocalDateTime ldt = LocalDateTime.parse( input );
Make that LocalDateTime an actual moment on the timeline by applying the intended time zone. If meant for UTC, make an Instant
.
Instant instant = ldt.toInstant( ZoneOffset.UTC );
If meant for a particular time zone, specify a ZoneId to get a ZoneDateTime.
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ldt.atZone( zoneId );