Java String To Date

Hey guys!! Welcome to flower brackets blog. Today we are going to learn java string to date.

java string to date

These are two important steps in string to date conversion in java.

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 – convert date to string java program

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 an example on how to convert string to date in java,

Example : java 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 in java - " + sdf.parse(strDate));
   }
}

Output:

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


Types of string to date conversion in java

Here is an example where you can find many types of string to date conversion in 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("String to date conversion in java");       
      System.out.println("---------------------------");      
      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:

String to date conversion in java
---------------------------
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


More on string to date in java

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 in java : " + reqFormat.format(dt));
   }
}

Output:

String to date in java : 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.

Related Posts