Java Reference
In-Depth Information
The method will try to parse the text as the specified types in order and return the first successful result. Typically,
a call to the parseBest() method is followed by a series of if-else statement with an instanceof operator to check
what type of object was returned. Listing 12-26 shows how to use the parseBest() method. Notice that the fourth text
is in invalid format and parsing it throws an exception.
Listing 12-26. Using the parseBest( ) Method of the DateTimeFormatter Class
// ParseBestTest.java
package com.jdojo.datetime;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAccessor;
public class ParseBestTest {
public static void main(String[] args) {
DateTimeFormatter parser = DateTimeFormatter.ofPattern("yyyy-MM-
dd['T'HH:mm:ss[Z]]");
parseStr(parser, "2012-05-31");
parseStr(parser, "2012-05-31T16:30:12");
parseStr(parser, "2012-05-31T16:30:12-0500");
parseStr(parser, "2012-05-31Hello");
}
public static void parseStr(DateTimeFormatter formatter, String text) {
try {
TemporalAccessor ta = formatter.parseBest(text,
OffsetDateTime::from,
LocalDateTime::from,
LocalDate::from);
if (ta instanceof OffsetDateTime) {
OffsetDateTime odt = OffsetDateTime.from(ta);
System.out.println("OffsetDateTime: " + odt);
} else if (ta instanceof LocalDateTime) {
LocalDateTime ldt = LocalDateTime.from(ta);
System.out.println("LocalDateTime: " + ldt);
} else if (ta instanceof LocalDate) {
LocalDate ld = LocalDate.from(ta);
System.out.println("LocalDate: " + ld);
} else {
System.out.println("Parsing returned: " + ta);
}
}
catch (DateTimeParseException e) {
System.out.println(e.getMessage());
}
}
}
 
Search WWH ::




Custom Search