Java String to Date

Java String to Date

We can convert String to Date in java using parse() method of DateFormat and SimpleDateFormat classes. To learn this concept well, you should visit DateFormat and SimpleDateFormat classes.

Java String to Date Example

Let’s see the simple code to convert String to Date in java.

  1. import java.text.SimpleDateFormat;
  2. import java.util.Date;
  3. public class StringToDateExample1 {
  4. public static void main(String[] args)throws Exception {
  5.     String sDate1=“31/12/1998”;
  6.     Date date1=new SimpleDateFormat(“dd/MM/yyyy”).parse(sDate1);
  7.     System.out.println(sDate1+“\t”+date1);
  8. }
  9. }

Output:

31/12/1998	Thu Dec 31 00:00:00 IST 1998

 

Let’s see another code to convert different types of string to Date in java.

  1. import java.text.SimpleDateFormat;
  2. import java.util.Date;
  3. public class StringToDateExample1 {
  4. public static void main(String[] args)throws Exception {
  5.     String sDate1=“31/12/1998”;
  6.     String sDate2 = “31-Dec-1998”;
  7.     String sDate3 = “12 31, 1998”;
  8.     String sDate4 = “Thu, Dec 31 1998”;
  9.     String sDate5 = “Thu, Dec 31 1998 23:37:50”;
  10.     String sDate6 = “31-Dec-1998 23:37:50”;
  11.     SimpleDateFormat formatter1=new SimpleDateFormat(“dd/MM/yyyy”);
  12.     SimpleDateFormat formatter2=new SimpleDateFormat(“dd-MMM-yyyy”);
  13.     SimpleDateFormat formatter3=new SimpleDateFormat(“MM dd, yyyy”);
  14.     SimpleDateFormat formatter4=new SimpleDateFormat(“E, MMM dd yyyy”);
  15.     SimpleDateFormat formatter5=new SimpleDateFormat(“E, MMM dd yyyy HH:mm:ss”);
  16.     SimpleDateFormat formatter6=new SimpleDateFormat(“dd-MMM-yyyy HH:mm:ss”);
  17.     Date date1=formatter1.parse(sDate1);
  18.     Date date2=formatter2.parse(sDate2);
  19.     Date date3=formatter3.parse(sDate3);
  20.     Date date4=formatter4.parse(sDate4);
  21.     Date date5=formatter5.parse(sDate5);
  22.     Date date6=formatter6.parse(sDate6);
  23.     System.out.println(sDate1+“\t”+date1);
  24.     System.out.println(sDate2+“\t”+date2);
  25.     System.out.println(sDate3+“\t”+date3);
  26.     System.out.println(sDate4+“\t”+date4);
  27.     System.out.println(sDate5+“\t”+date5);
  28.     System.out.println(sDate6+“\t”+date6);
  29. }
  30. }

Output:

31/12/1998	Thu Dec 31 00:00:00 IST 1998
31-Dec-1998	Thu Dec 31 00:00:00 IST 1998
12 31, 1998	Thu Dec 31 00:00:00 IST 1998
Thu, Dec 31 1998	Thu Dec 31 00:00:00 IST 1998
Thu, Dec 31 1998 23:37:50	Thu Dec 31 23:37:50 IST 1998
31-Dec-1998 23:37:50	Thu Dec 31 23:37:50 IST 1998
Next TopicJava float to String
java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Java String to Date We can convert String to Date in java using parse() method of DateFormat and SimpleDateFormat classes. To learn this concept well, you should visit DateFormat and SimpleDateFormat classes. Java String to Date Example Let’s see the simple code to convert String to Date in java. import java.text.SimpleDateFormat; import java.util.Date; public class StringToDateExample1 { public static void main(String[] args)throws Exception {     String sDate1=“31/12/1998”;     Date date1=new SimpleDateFormat(“dd/MM/yyyy”).parse(sDate1);     System.out.println(sDate1+“\t”+date1); } } Output: […]

Tags:

....
...