Java 8 ResolverStyle.STRICT

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 string to dates in Java happen 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 example

Java program to parse a date string to LocalDate instance using strict format. It shall give error 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 example

Java program to parse a date time string to LocalDateTime instance using 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 check if string contains date in strict mode.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.