Java Convert String To Date

Sharing is healthy !!

Hey guys!! Welcome to flower brackets. Today we are going to learn java 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.

These are two important steps while converting string to date.

Now let’s see an example on how to convert a string to date in java,

example: Java Convert String To Date

Java Convert String To Date

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class StringToDate
{
   public static void main(String[] args) throws ParseException  
   {
      SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
      String strDate = "23/05/2014 05:10:14";   
      System.out.println("String to date conversion - " + sdf.parse(strDate));
   }
}

Output:

String to date conversion – Fri May 23 05:10:14 IST 2014


types of string to date conversions

Here is an example where you can find many types of string to date conversions.

Also read – convert date to string java program

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";
      
      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");
      
      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);      

      System.out.println("Java Convert String To Date");       
      System.out.println("---------------------------");      
      System.out.println(strDate1 + "\t" + dt1);         
      System.out.println(strDate2 + "\t" + dt2);         
      System.out.println(strDate3 + "\t" + dt3);        
      System.out.println(strDate4 + "\t" + dt4);         
      System.out.println(strDate5 + "\t" + dt5);        
      System.out.println(strDate6 + "\t" + dt6);
      System.out.println(strDate7 + "\t" + dt7);
      System.out.println(strDate8 + "\t" + dt8);
      System.out.println(strDate9 + "\t" + dt9);
      System.out.println(strDate10 + "\t" + dt10);
   }
}

Output:

Java Convert String To Date
---------------------------
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 Thu Jan 23 00:05:00 IST 2014

More on converting string to date

Here is an example where a string to converted to date

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

public class StringToDateExample
{
   public static void main(String[] args) throws ParseException  
   { 
      SimpleDateFormat givenFormat = new SimpleDateFormat("yyyyMMddHHmmss");
      SimpleDateFormat reqFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      Date dt = givenFormat.parse("20140523051014");   
      System.out.println("String to date conversion : " + reqFormat.format(dt));
   }
}

Output:

String to date conversion : 2014-05-23 05:10:14


conclusion

That’s it guys. This is all about how to convert a string to date in java with an example.

I hope you have understood the concept. You can subscribe to my blog flower brackets if you haven’t already.

Do share this article if you like.

Sharing is healthy !!