I have this format:
2011-10-10T01:45:20+00:00
I tried using LocalDateTime.parse("2011-10-10T01:45:20+00:00")
but I got error:
java.time.format.DateTimeParseEception: Text '2011-10-10T01:45:20+00:00' could not be parse, unparsed text found
I have this format:
2011-10-10T01:45:20+00:00
I tried using LocalDateTime.parse("2011-10-10T01:45:20+00:00")
but I got error:
java.time.format.DateTimeParseEception: Text '2011-10-10T01:45:20+00:00' could not be parse, unparsed text found
The default formatter is DateTimeFormatter.ISO_LOCAL_DATE_TIME : '2011-12-03T10:15:30'
, the offset is not in,
You may parse using OffsetDateTime
class that uses DateTimeFormatter.ISO_OFFSET_DATE_TIME : '2011-12-03T10:15:30+01:00'
as formatter
OffsetDateTime.parse("2011-10-10T01:45:20+00:00") // print 2011-10-10T01:45:20Z
You can still use LocalDateTime
but you need to specify the formatter
LocalDateTime.parse("2011-10-10T01:45:20+00:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME); // 2011-10-10T01:45:20
OffsetDateTime.parse( "2011-10-10T01:45:20+00:00" )
The LocalDateTime
class is not appropriate to your input. That class represents only a date and a time-of-day but without any offset-from-UTC or time zone, so it does not represent a moment, is not a point on the timeline.
Your input string in contrast represents a moment, with an offset-from-UTC of zero hours-minutes-seconds: +00:00
OffsetDateTime
Correct class for your input is OffsetDateTime
.
Your input string is in standard ISO 8601 format. These standard formats are used by default in the java.time classes. So no need to specify a formatting pattern.
OffsetDateTime odt = OffsetDateTime.parse( "2011-10-10T01:45:20+00:00" ) ;
See this code run live at IdeOne.com.
odt.toString(): 2011-10-10T01:45:20Z
FYI, the difference between offset and zone:
ZoneOffset
class.Continent/Region
format such as America/Montreal
or Africa/Tunis
. Represented by ZoneId
class.You could provide ISO date formatter in the parse method like this
import java.time.format.DateTimeFormatter
LocalDate.parse("2011-10-10T01:45:20+00:00", DateTimeFormatter.ISO_DATE_TIME)