Java convert string to date

Let’s learn java convert string to date.

java convert string to date

Java convert string to date

Below are two important steps to convert string to date. First we have to create simple date pattern object by passing the date pattern.

Then we need to pass the date string to SimpleDateFormat parse method.

Also read – sorting hashmap by values in java

Here we have some date and time patterns used in java.text.SimpleDateFormat,

LetterDescriptionExamples
yYear2014
MMonth in yearJune, 06, 6
dDay in month1 - 31
EDay name in weekMonday, Friday
aam/pm markerAM, PM
HHour in a day0-23
hhour in am/pm1 - 12
mMinute in hour0-60
sSecond in minute0-60

Now let’s see java program,

// string to date java
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Demo
{
   public static void main(String[] args) throws ParseException  
   {
      String strDate = "09/03/1996";
      // simpledateformat parse
      Date dt = new SimpleDateFormat("dd/MM/yyyy").parse(strDate);  
      System.out.println(strDate + "\t" + dt);
   }
}



Output:

09/03/1996                 Sat Mar 09 00:00:00 IST 1996


Here is an example where you can find dateformats,

// string to date java
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class StringToDateDemo
{
   public static void main(String[] args) throws ParseException  
   {
      String strDate1 = "23/05/2014";
      String strDate2 = "23-May-2014";
      String strDate3 = "05 23, 2014";
      String strDate4 = "Fri, May 23 2014";
      String strDate5 = "Fri, May 23 2014 17:10:14";        
      String strDate6 = "23-May-2014 17:10:14";
      String strDate7 = "May 23, 2014";
      String strDate8 = "05/23/14 5:10 am";
      String strDate9 = "May 23, 2014 5:10:14 pm";
      String strDate10 = "2014-05-23";
      String strDate11 = "Friday, Jun 7, 2013 12:10:56 PM";
      String strDate12 = "2014-10-05T15:23:01Z";
      
      SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy");
      SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MMM-yyyy");
      SimpleDateFormat sdf3 = new SimpleDateFormat("MM dd, yyyy");
      SimpleDateFormat sdf4 = new SimpleDateFormat("E, MMM dd yyyy");
      SimpleDateFormat sdf5 = new SimpleDateFormat("E, MMM dd yyyy HH:mm:ss");
      SimpleDateFormat sdf6 = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
      SimpleDateFormat sdf7 = new SimpleDateFormat("MMMM d, yyyy");
      SimpleDateFormat sdf8 = new SimpleDateFormat("MM/dd/yy h:mm a");
      SimpleDateFormat sdf9 = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a");
      SimpleDateFormat sdf10 = new SimpleDateFormat("yyyy-MM-dd");
      SimpleDateFormat sdf11 = new SimpleDateFormat("EEEE, MMM dd, yyyy HH:mm:ss a");
      // Here SimpleDateFormat do not parse correctly
      // we have to replace suffix Z with ‘+0000’
      SimpleDateFormat sdf12 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
      
      Date dt1 = sdf1.parse(strDate1);
      Date dt2 = sdf2.parse(strDate2);
      Date dt3 = sdf3.parse(strDate3);
      Date dt4 = sdf4.parse(strDate4);
      Date dt5 = sdf5.parse(strDate5);
      Date dt6 = sdf6.parse(strDate6);
      Date dt7 = sdf7.parse(strDate7);
      Date dt8 = sdf8.parse(strDate8);
      Date dt9 = sdf9.parse(strDate9);
      Date dt10 = sdf10.parse(strDate10);
      Date dt11 = sdf11.parse(strDate11);
      // here "Z" means UTC 
      Date dt12 = sdf12.parse(strDate12.replaceAll("Z$", "+0000"));   
    
      System.out.println(strDate1 + " - " + dt1);         
      System.out.println(strDate2 + " - " + dt2);         
      System.out.println(strDate3 + " - " + dt3);        
      System.out.println(strDate4 + " - " + dt4);         
      System.out.println(strDate5 + " - " + dt5);        
      System.out.println(strDate6 + " - " + dt6);
      System.out.println(strDate7 + " - " + dt7);
      System.out.println(strDate8 + " - " + dt8);
      System.out.println(strDate9 + " - " + dt9);
      System.out.println(strDate10 + " - " + dt10);
      System.out.println(strDate11 + " - " + dt11);
      System.out.println(strDate12 + " - " + dt12);
      System.out.println("Time zone : " + TimeZone.getDefault().getID());
   }
}


Output:

23/05/2014 - Fri May 23 00:00:00 IST 2014
23-May-2014 - Fri May 23 00:00:00 IST 2014
05 23, 2014 - Fri May 23 00:00:00 IST 2014
Fri, May 23 2014 - Fri May 23 00:00:00 IST 2014
Fri, May 23 2014 17:10:14 - Fri May 23 17:10:14 IST 2014
23-May-2014 17:10:14 - Fri May 23 17:10:14 IST 2014
May 23, 2014 - Fri May 23 00:00:00 IST 2014
05/23/14 5:10 am - Fri May 23 05:10:00 IST 2014
May 23, 2014 5:10:14 pm - Fri May 23 17:10:14 IST 2014
2014-05-23 - Fri May 23 00:00:00 IST 2014
Friday, Jun 7, 2013 12:10:56 PM - Fri Jun 07 12:10:56 IST 2013
2014-10-05T15:23:01Z - Sun Oct 05 20:53:01 IST 2014
Time zone : Asia/Calcutta