Java convert string to date

Let’s learn java convert string to date.

Java convert string to date

Java string to date conversion is one of the common java interview question.

In this post let us learn how to convert string to date.

Also read – sorting hashmap by values in java

Here is the table where 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 convert string to date.

import java.util.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ConvertStringToDate
{
   public static void main(String[] args) throws Exception
   {
      String strDate = "09/03/1996";
      Date dt = new SimpleDateFormat("dd/MM/yyyy").parse(strDate);
      System.out.println(strDate + " - " + dt);
   }
}


Output:

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


Here is an example where you can find dateformats.

import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
public class StringToDateDemo
{
   public static void main(String[] args) throws Exception
   {
      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("Time zone : " + TimeZone.getDefault().getID());
      System.out.println(strDate12 + " - " + dt12);
   }
}


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
Time zone : Asia/Calcutta
2014-10-05T15:23:01Z – Sun Oct 05 20:53:01 IST 2014


how to convert yyyy-mm-dd format

Let’s learn how to convert string to date in java in yyyy-mm-dd format.

import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateExample 
{
   public static void main(String[] args) throws ParseException 
   {
      String strDate = "2006-05-25";
      Date dt = new SimpleDateFormat("yyyy-mm-dd").parse(strDate);
      System.out.println(strDate + " - " + dt);
   }
}


Output:

2006-05-25 – Wed Jan 25 00:05:00 IST 2006


string to date java 8

Let’s see example in converting String to Date in java 8 using LocalDate class and DateTimeFormatter class.

Syntax:

LocalDate ld = LocalDate.parse(date, formatter);

parse() method returns the parsed local date, not null.

import java.time.LocalDate;
import java.time.LocalDateTime;
public class Java8StringToDate
{
   public static void main(String[] args)
   {
      DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("d/MM/yyyy");
      String strDate1 = "14/05/2005";
      LocalDate ld1 = LocalDate.parse(strDate1, dtf1); 
      System.out.println(ld1);
      DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("MMM d yyyy");
      String strDate2 = "May 14 2005";
      LocalDate ld2 = LocalDate.parse(strDate2, dtf2); 
      System.out.println(ld2);
   }
}


Output:

2005-05-14
2005-05-14


This is all about convert string to date in java with example.


Also read – preface to java virtual machine and architecture