3

I was trying to block incompatible date in input. So I intentionally gave wrong date as string. I set calendar.setLenient to false hoping that it would not allow date to parse. But It pass through it. Below is my code:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Test {
    public static void main(String[] args) throws Exception{
        try {
            String from ="2018-15-18";
            String to = "2018-15-18";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calender1 = Calendar.getInstance();
            Calendar calender2 = Calendar.getInstance();
            calender1.setLenient(false);
            calender2.setLenient(false);

            calender1.setTime(sdf.parse(from));
            calender2.setTime(sdf.parse(to));

            Date dtFrom = calender1.getTime();
            Date dtTo = calender2.getTime();

            System.out.println(sdf.format(dtFrom));


            if((from!=null && !from.isEmpty())&&(to!=null && !to.isEmpty())&&(dtFrom!=null && dtTo!=null))
                System.out.println("ok");
            else
                System.out.println("not ok");
        }catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}

As I have given wrong date i.e. month 15 does not exist. So I was expecting exception on line calender1.setTime(sdf.parse(from)); but it pass through and printing date value as : 2019-03-18. I did not wanted that. Why is it so? How can I make sure wrong date input should not be entertained further in my code once it is not able to parse.

J. Doe
  • 69
  • 7
  • See https://stackoverflow.com/questions/7606387/what-is-the-use-of-lenient – Ori Marko Nov 27 '18 at 07:10
  • 3
    You need to `setLenient(false);` for the `SimpleDateFormat` as this is the instance to parse the string. `sdf.parse(from)` returns already a proper `Date` which can't throw an exception anymore. If you only `setLenient(false);` for the calendar. then it only throws an exception on something like `calender1.set(2018, 15, 18);` – XtremeBaumer Nov 27 '18 at 07:12
  • Obligatory note: If you are using some version of java 8 or newer, please don't use `Calendar` - it has known issues and has been replaced by the classes in https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html – Hulk Nov 27 '18 at 07:12
  • 1
    Just a remark: 3=15 mod 12. Apparantly when you give a false date, Java uses modulo counting for getting the right one. – Dominique Nov 27 '18 at 07:14
  • @XtremeBaumer your comment resolved my issue. thank you. – J. Doe Nov 27 '18 at 07:16
  • @Dominique ok. I will remember that. thanks. – J. Doe Nov 27 '18 at 07:17
  • FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Nov 27 '18 at 07:37

0 Answers0