A lenient DateFormat

If DateFormat is not lenient, the Date supplied has to be a valid one. If it is lenient, you can work with dates that are out of range. Eg. Dec. 40th, 2000 will refer to the date Jan 9th, 2001.

Main.java:

import java.util.*;
import java.text.*;
    
public class Main {
   public static void main(String args[]) {
      try {
         Date date;
 
         DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
         date = df.parse("12/40/00");   
         System.out.println("default:t" + date);
 
         df.setLenient(true);
         date = df.parse("12/40/00");   
         System.out.println("lenient:t" + date);
 
         df.setLenient(false);
         date = df.parse("12/40/00");   
         System.out.println("not lenient:t" + date);
      }
      catch(ParseException e) {
         e.printStackTrace();
      }
   }
}

outputs:

default:        Tue Jan 09 00:00:00 CET 2001
lenient:        Tue Jan 09 00:00:00 CET 2001
java.text.ParseException: Unparseable date: "12/40/00"
        at java.text.DateFormat.parse(DateFormat.java:331)
        at Main.main(Main.java:18)