ThreeTenABP: How to validate date using a custom date format / DateTimeFormatter?

Tom

I'm using ThreeTenABP and seem to have run into a difference of implementation between LocalDate.parse(String) and LocalDate.parse(String, DateTimeFormatter).

LocalDate.parse("31/02/1985", DateTimeFormatter.ofPattern("dd/MM/yyyy"))

Parses to "1985-02-28" without throwing an exception.

LocalDate.parse("2015-02-31")

DateTimeParseException: Text '2015-02-31' could not be parsed: Invalid date 'FEBRUARY 31'

The documentation almost implies this with "The string must represent a valid date" only mentioned with the formatter-less method.

How can I validate a date in a custom format like 31/02/1985 using threeten bp?

Meno Hochschild

The main difference can be explained by the fact that the ISO_LOCAL_DATE-formatter is strict by default. Other formatters are smart by default. The full sentence you cited reads like this:

The string must represent a valid date and is parsed using DateTimeFormatter.ISO_LOCAL_DATE.

So it is pretty clear that the formatter-less method can only parse ISO-compatible dates in strict mode, and even then only a subset of ISO-8601, namely:

uuuu-MM-dd or uuuuMMdd

About the strict mode, you can see it studying the source code:

   public static final DateTimeFormatter ISO_LOCAL_DATE; 
   static { 
     ISO_LOCAL_DATE = new DateTimeFormatterBuilder() 
       .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD) 
       .appendLiteral('-') 
       .appendValue(MONTH_OF_YEAR, 2) 
       .appendLiteral('-') 
       .appendValue(DAY_OF_MONTH, 2) 
       .toFormatter(ResolverStyle.STRICT).withChronology(IsoChronology.INSTANCE); 
     } 

However, the strict mode does not seem to be well documented. Anyway, if you want to realize the strict mode with a custom formatter then simply call its method withResolverStyle(ResolverStyle.STRICT).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to convert ZonedDateTime/OffsetDateTime to Date using ThreeTenABP?

From Java

Date Format Issue With DateTimeFormatter

From Dev

How to convert from Date to LocalDate when using ThreeTenABP?

From Dev

how to validate UTC format date

From Dev

how to validate UTC format date

From Dev

moment to validate date and time with custom format

From Dev

How to display date using custom date format as per user locale

From Dev

How to print a date using DateTimeFormatter.ISO_LOCAL_DATE?

From Dev

validate date format using jqueryUI parseDate method

From Dev

How to validate date using PHP?

From Dev

How to validate HTML5 date format

From Dev

How to validate the date format of a column in Pyspark?

From Dev

Model validate date format

From Dev

Validate a Date format in Ruby

From Dev

format date using moment with custom format

From Dev

Validate date in dd/mm/yyyy format using JQuery Validate

From Dev

Parsing a date using DateTimeFormatter ofPattern

From Dev

How can I change java.util.Date to ISO String using ThreeTenABP

From Dev

Elasticsearch cannot parse date using custom format

From Dev

how to format date using SimpleDateFormat

From Dev

How to format a date using StringBuilder

From Dev

how to format date using javascript

From Dev

Android Date Format into custom date

From Dev

Validate date format in a shell script

From Dev

RegEx to validate Date Format Masks

From Dev

Restrict and validate date format in Excel

From Dev

Regex to validate date in PHP using format as YYYY-MM-DD

From Dev

How to format php date using this format

From Dev

How to validate date within current year using jQuery Validate plugin

Related Related

HotTag

Archive