Java Strict, Smart and Lenient Date Resolutions

Learn to parse date and time strings into instances of LocalDate and LocalDateTime using strict style using ResolverStyle.STRICT parameter with DateTimeFormatter instance.

1. ResolverStyle – Parsing styles

Parsing a string to date in Java happens in two phases:

  • Phase 1 is a basic text parse according to the fields added to the builder. It simply checks that individual fields in date string (e.g. day, month, year etc) make sense.
  • Phase 2 resolves the parsed field-value pairs into date and/or time objects. Here Java tries to create actual date time objects with supplied information in date string.

ResolverStyle is an enum and used to control how phase 2, resolving, happens. It contains three styles of parsing:

  • LENIENT – Style to resolve dates and times leniently.
2019-02-27  - is parsed to  - 2019-02-27
2019-02-28  - is parsed to  - 2019-02-28
2019-02-29  - is parsed to  - 2019-03-01  //Date moved to next month
  • SMART – Style to resolve dates and times in a smart, or intelligent, manner.
2019-02-27  - is parsed to  - 2019-02-27
2019-02-28  - is parsed to  - 2019-02-28
2019-02-29  - is parsed to  - 2019-02-28  //Date adjusted based on smart guessing
  • STRICT – Style to resolve dates and times strictly.
2019-02-27  - is parsed to  - 2019-02-27
2019-02-28  - is parsed to  - 2019-02-28
2019-02-29  - is parsed to  - //java.time.format.DateTimeParseException

2. Strict LocalDate Parsing

Java program to parse a date string to LocalDate instance using the strict format. It shall give errors on invalid dates such as 30th February.

Java 8 uses 'uuuu' for year, not 'yyyy'. In Java 8, ‘yyyy’ means “year of era” (BC or AD).

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.ResolverStyle;

public class Main
{
	static DateTimeFormatter DATE_FORMATTER
						= DateTimeFormatter.ofPattern("uuuu-MM-dd");

	public static void main(String[] args)
	{
		System.out.println("Parsed Date :: " + parseDate("2019-02-27"));
		System.out.println("Parsed Date :: " + parseDate("2019-02-28"));
		System.out.println("Parsed Date :: " + parseDate("2019-02-29"));
	}

	private static LocalDate parseDate(String dateString)
	{
		LocalDate parsedDate = LocalDate.parse(dateString,
				DATE_FORMATTER.withResolverStyle(ResolverStyle.STRICT));

		return parsedDate;
	}
}]

Program output.

Parsed Date :: 2019-02-27
Parsed Date :: 2019-02-28
Exception in thread "main" java.time.format.DateTimeParseException:
		Text '2019-02-29' could not be parsed: Invalid date 'February 29' as '2019' is not a leap year
	at java.time.format.DateTimeFormatter.createError(Unknown Source)

3. Strict LocalDateTime Parsing

Java program to parse a date-time string to LocalDateTime instance using the strict format.

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.ResolverStyle;

public class Main
{
	static DateTimeFormatter DATE_FORMATTER
						= DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSS");

	public static void main(String[] args)
	{
		System.out.println("Parsed DateTime :: " + parseDateTime("2019-02-27T11:23:56.1234"));
		System.out.println("Parsed DateTime :: " + parseDateTime("2019-02-28T11:23:56.1234"));
		System.out.println("Parsed DateTime :: " + parseDateTime("2019-02-29T11:23:56.1234"));
	}

	private static LocalDateTime parseDateTime(String dateString)
	{
		LocalDateTime parsedDateTime = LocalDateTime.parse(dateString,
				DATE_FORMATTER.withResolverStyle(ResolverStyle.STRICT));

		return parsedDateTime;
	}
}

Program output.

Parsed DateTime :: 2019-02-27T11:23:56.123400
Parsed DateTime :: 2019-02-28T11:23:56.123400
Exception in thread "main" java.time.format.DateTimeParseException:
		Text '2019-02-29T11:23:56.1234' could not be parsed: Invalid date 'February 29' as '2019' is not a leap year
	at java.time.format.DateTimeFormatter.createError(Unknown Source)

Drop me your questions in comments related to checking if a string contains the date in strict or smart mode.

Happy Learning !!

Leave a Reply

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.

Our Blogs

REST API Tutorial