Reputation: 39
The following works perfectly:
String startDate = "2019-10-12T00:00:00.000-07:00"
LocalDateTime startDateTime = LocalDateTime.parse(startDate,
DateTimeFormatter.ISO_ZONED_DATE_TIME);
However, for the following code:
String startDate = "2019-10-12T00:00:00.000+07:00"
LocalDateTime startDateTime = LocalDateTime.parse(startDate,
DateTimeFormatter.ISO_ZONED_DATE_TIME);
I get an exception:
throws
java.time.format.DateTimeParseException
: Text '2019-10-12T00:00:00.000 07:00' could not be parsed at index 23
Can someone help me understand what might be possibly wrong here?
Upvotes: 1
Views: 1382
Reputation: 340178
(A) As mentioned in comment by Andreas, the disappearance of your +
character in error message is suspicious. You likely have a character encoding problem. We here do not have enough info to diagnose that.
(B) Use OffsetDateTime
rather than LocalDateTime
, to fit your data.
OffsetDateTime
.parse( "2019-10-12T00:00:00.000+07:00" )
.toInstant()
.toString()
2019-10-11T17:00:00Z
See this code run live at IdeOne.com.
LocalDateTime
is the wrong classThe LocalDateTime
class lacks any concept of time zone or offset-from-UTC. So it cannot, by definition, represent a moment. A LocalDateTime
object is not a point on the timeline.
Yet your input string indicates an offset-from-UTC, the +07:00
in 2019-10-12T00:00:00.000+07:00
. Your attempt to put this value in a LocalDateTime
is a misfit, a square peg in a round hole.
Never use LocalDateTime
when you mean a specific moment, a specific point on the timeline. This class is almost never used in business-oriented apps for present or past moments. When making appointments in the future, this class should be used when you want the time-of-day to remain the same regardless of politicians redefining the region’s time zone.
OffsetDateTime
Parse as an OffsetDateTime
object.
Your input string happens to comply fully with the ISO 8601 standard for textual date-time formats. The java.time classes use these formats by default when parsing/generating strings. So no need to specify a formatting pattern.
String input = "2019-10-12T00:00:00.000+07:00" ;
OffsetDateTime odt = OffsetDateTime.parse( input ) ;
To adjust to the wall-clock time of UTC (an offset of zero), call OffsetDateTime::withOffsetSameInstant
. For your convenience, use the constant ZoneOffset.UTC
.
OffsetDateTime odtUtc = odt.withOffsetSameInstant( ZoneOffset.UTC ) ;
Or simply extract an object of the basic building-block class in java.time, an Instant
. An Instant
is always in UTC, by definition.
Instant instant = odt.toInstant() ;
ZonedDateTime
To see the same moment as viewed through the wall-clock time used by the people of a particular region (a time zone), apply a ZoneId
to get a ZonedDateTime
.
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = odt.atZoneSameInstant( z ) ;
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
Upvotes: 6
Reputation: 364
I tried running both dates locally with the same parsing of the LocalDateTime
, and they both run correctly.
I could recreate your same error by using a different date (note the space at the zone offset, instead of a +
or -
) 2019-10-12T00:00:00.000 07:00
instead of 2019-10-12T00:00:00.000+07:00
. Which is what your error message shows (but not what your code has).
Are you sure that the code version you show, which uses 2019-10-12T00:00:00.000+07:00
, is the actual version that produces the error? It looks like you have been using 2019-10-12T00:00:00.000 07:00
, which then leads to the error.
Upvotes: 4
Reputation: 11
there is an extra zero in the startDateTime use startDateTime = "2019-10-12T00:00:00.00+07:00"
Upvotes: -2