1

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
Ari
  • 45
  • 4
Jack Thomson
  • 193
  • 8

3 Answers3

3

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

Oracle Documentation

azro
  • 53,056
  • 7
  • 34
  • 70
3

tl;dr

OffsetDateTime.parse( "2011-10-10T01:45:20+00:00" )

Wrong class

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

Table of date-time types in Java, both modern and legacy.

FYI, the difference between offset and zone:

  • An offset-from-UTC is merely a number of hours-minutes-seconds. Nothing more. Represented by ZoneOffset class.
  • A time zone is much more. A time zone is a history of past, present, and future changes to the offset used by the people of a particular region. A time zone carries a name in Continent/Region format such as America/Montreal or Africa/Tunis. Represented by ZoneId class.
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • `Modern` refers to `since Java 1.8`, yes? – second Aug 19 '19 at 18:42
  • 1
    @second If you mean does this require Java 8, yes, the *java.time* classes defined in JSR 310 appear in Java 8 and later, with a back-port to Java 6 and Java 7 available in the [*ThreeTen-Backport*](https://www.threeten.org/threetenbp/) project. But, no, my use of "modern" here refers to the elegant design of the library using lessons learned from its predecessor, the *Joda-Time* project, and the anti-lessons learned from the terrible legacy classes. One example: *java.time* is entirely read-only, using immutable objects, as is appropriate to this kind of value-oriented content. – Basil Bourque Aug 19 '19 at 18:49
  • @BasilBourque how can I get the string format out of the OfsetDateTime? (I want it in the same format as this 2011-10-10T01:45:20+00:00 – Jack Thomson Aug 19 '19 at 22:56
  • @JackThomson Do you understand that the `Z` on the end means `+00:00`? Pronounced “Zulu”, it is a common shortcut for an offset of zero hours-minutes-seconds, and is officially defined in the ISO 8601 standard. If you insist on the `+00:00`, use the `DateTimeFormatter` class to generate a string. This has been covered many many times already, so I'll not cover it here. Search Stack Overflow, and study the formatting pattern codes in the Javadoc. – Basil Bourque Aug 20 '19 at 00:37
1

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)
user1625348
  • 91
  • 1
  • 7