Using SimpleDateFormat to convert String to Date - pass date, month, year, hour, minute, second, day, Am/Pm(E), time zone(z) || Enabling strict parsing of String by setting SimpleDateFormat’s setLenient(false) in java


Contents of page :
  • Program 1) Using SimpleDateFormat to convert String to Date (use dd-MM-yyyy format)

  • Program 2) Using SimpleDateFormat to convert String to Date (use dd-MM-yyyy hh:mm:ss format)



  • Program 3) Using SimpleDateFormat to convert String to Date - pass date, month, year, hour, minute, second, day, Am/Pm(use   
E dd-MM-yyyy hh:mm:ss a    format)

  • Program 4) Using SimpleDateFormat to convert String to Date - pass date, month, year, hour, minute, second, day, time zone(z) (use   
E MM dd hh:mm:ss z yyyy format)

  • Program 5) Using SimpleDateFormat to convert String to Date - setting lenient false by using setLenient(false) - use dd-MM-yyyy format

  • Program 6.1) Using SimpleDateFormat to convert String to Date - lenient is true by default
  • Program 6.2) Using SimpleDateFormat to convert String to Date - lenient is true by default

Program 1) Using SimpleDateFormat to convert String to Date (use dd-MM-yyyy format)

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

/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String... args) {
          try {
                 SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                 Date date = sdf.parse("21-02-2015");
                 System.out.println("date = " + date);
          } catch (ParseException pe) {
                 pe.printStackTrace();
          }
   }
}
/*OUTPUT
date = Sat Feb 21 00:00:00 IST 2015
*/

I have mentioned few format that could be used>
Use this format
Pass this String
dd-MM-yyyy
Or
d-m-y
21-02-2015
dd_MM_yyyy   
21_02_2015
dd MM yyyy   
21 02 2015
ddMMyyyy
21022015
dd/MM/yyyy   
21/02/2015

for complete list format and combinations that could be used



Program 2) Using SimpleDateFormat to convert String to Date (use dd-MM-yyyy hh:mm:ss format)

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String... args) {
          try {
                 SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
                 Date date = sdf.parse("21-12-2015 10:54:46");
                 System.out.println("date = "+date);
          } catch (ParseException pe) {
                 pe.printStackTrace();
          }
   }
}
/*OUTPUT
date = Mon Dec 21 10:54:46 IST 2015
*/


I have mentioned few format that could be used>
Use this format
Pass this String
dd-MM-yyyy hh:mm:ss
Or
dd-MM-yyyy h:m:s
21-02-2015 10:54:46
dd-MM-yyyy hhmmss
21-02-2015 105446
dd-MM-yyyy hh-mm-ss
21-02-2015 10-54-46
dd-MM-yyyy hh mm ss
21-02-2015 10 54 46
dd-MM-yyyy hh/mm/ss
21-02-2015 10/54/46

for complete list format and combinations that could be used


Program 3) Using SimpleDateFormat to convert String to Date - pass date, month, year, hour, minute, second, day, Am/Pm(use   
E dd-MM-yyyy hh:mm:ss a   
format)

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String... args) {
          try {
                 SimpleDateFormat sdf = new SimpleDateFormat("E dd-MM-yyyy hh:mm:ss a");
                 Date date = sdf.parse("Mon 21-12-2015 10:54:46 PM");
                 System.out.println("date = "+date);
          } catch (ParseException pe) {
                 pe.printStackTrace();
          }
   }
}
/*OUTPUT
date = Mon Dec 21 22:54:46 IST 2015
*/


Program 4) Using SimpleDateFormat to convert String to Date - pass date, month, year, hour, minute, second, day, time zone(z) (use   
E MM dd hh:mm:ss z yyyy
format)
Here we are trying to send String in format in which it is printed when java.util.Date is used in sysout statement.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String... args) {
          try {
                 Date date = new Date();
                 System.out.println("date =     "+ date);               
                 SimpleDateFormat sdf = new SimpleDateFormat("E MM dd hh:mm:ss z yyyy");
                 date = sdf.parse("Mon 12 21 10:54:46 IST 2015");
                 System.out.println("manual date = "+date);
          } catch (ParseException pe) {
                 pe.printStackTrace();
          }
   }
}
/*OUTPUT
date =     Wed Jul 08 15:11:31 IST 2015
manual date = Mon Dec 21 10:54:46 IST 2015
*/





Program 5) Using SimpleDateFormat to convert String to Date - setting lenient false by using setLenient(false) - use dd-MM-yyyy format
By default lenient is true, so String passed to be converted in Date are not validated (we will see impact of using lenient as true in Program 6.1 and 6.2),
but by setting lenient false, if any invalid String passed to be converted in Date than ParseException is thrown.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String... args) {
          try {
                 SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                 sdf.setLenient(false); //by default lenient is true,
                 Date date = sdf.parse("29-02-2015");  //Now, lenient is false,
                                                                              //So, ParseException will be thrown at RunTime.
                 System.out.println("date = "+date);
          } catch (ParseException pe) {
                 pe.printStackTrace();
          }
   }
}
/*OUTPUT
java.text.ParseException: Unparseable date: "29-02-2015"
   at java.text.DateFormat.parse(Unknown Source)
   at DateTest.main(DateTest.java:14)
*/



Program 6.1) Using SimpleDateFormat to convert String to Date - lenient is true by default
By default lenient is true, so String passed to be converted in Date are not validated. So, in this program "29-02-2015" is converted to "1-03-2015"

But we saw in Program 5 that "29-02-2015" is invalid date, and setting lenient false threw ParseException
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String... args) {
          try {
                 SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                 Date date = sdf.parse("29-02-2015"); //by default lenient is true,
                                                    //So, date will become 1 Mar 2015
                 System.out.println("date = "+date);
          } catch (ParseException pe) {
                 pe.printStackTrace();
          }
   }
}
/*OUTPUT
date = Sun Mar 01 00:00:00 IST 2015
*/


Program 6.2) Using SimpleDateFormat to convert String to Date - lenient is true by default
By default lenient is true, so String passed to be converted in Date are not validated. So, in this program "32-13-2015" is converted to "1-02-2016"

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String... args) {
          try {
                 SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                 Date date = sdf.parse("32-13-2015"); //by default lenient is true,
                                                     //So, date will become 1 Feb 2016
                 System.out.println("date = "+date);
          } catch (ParseException pe) {
                 pe.printStackTrace();
          }
   }
}
/*OUTPUT
date = Mon Feb 01 00:00:00 IST 2016
*/




RELATED LINKS>
Creating Date >

Creating Date in java using Calendar, GregorianCalendar and java.util.Date | Getting Year, Month, Date, Hour, Minute, Second from current date



Difference between two Dates>

Difference between two dates in days, hours, minutes, seconds, milliSeconds- where dates are manually created using dd-MM-yyyy and dd-MM-yyyy hh:mm:ss format



eEdit
Must read for you :