OrdinaryNick
OrdinaryNick

Reputation: 248

Joda time parsing time with more than 24 hour

Is there possible parse time with date like "2016-07-13T25:50" to get LocalDateTime with value "2016-07-14T01:50"? I want to use only Joda time and java.util.

Upvotes: 4

Views: 1170

Answers (2)

Meno Hochschild
Meno Hochschild

Reputation: 44061

You can use a lenient chronology in Joda-Time:

LenientChronology lenientChrono = LenientChronology.getInstance(ISOChronology.getInstance());
DateTimeFormatter formatter = 
  DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm").withChronology(lenientChrono);
LocalDateTime ldt = formatter.parseLocalDateTime("2016-07-13T25:50");
System.out.println("lenient=" + ldt); // 2016-07-14T01:50:00.000

Upvotes: 5

xea
xea

Reputation: 1214

It does not seem possible without writing a custom parser becase Joda and the Java 8 DateTime API (which is largely inspired by Joda) works with the ISO-8601 calendar system and that doesn't allow this format.

Quote from Wikipedia regarding durations:

Alternatively, a format for duration based on combined date and time representations may be used by agreement between the communicating parties either in the basic format PYYYYMMDDThhmmss or in the extended format P[YYYY]-[MM]-[DD]T[hh]:[mm]:[ss]. For example, the first duration shown above would be "P0003-06-04T12:30:05". However, individual date and time values cannot exceed their moduli (e.g. a value of 13 for the month or 25 for the hour would not be permissible).

A list of available DateTimeFormatters is available in the API docs.

Upvotes: 2

Related Questions