Overview
In this tutorial, we show you how to convert String to Date in Java with
java.util.Date and
java.text.SimpleDateFormat.
The format is dd-MMM-yyyy. The String is 15-Jul-2018
package com.jackrutorial;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateExample1 {
public static void main(String[] args) throws ParseException {
String str = "15-Jul-2018";
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
Date date = formatter.parse(str);
System.out.println(date);
System.out.println(formatter.format(date));
}
}
Output
Sun Jul 15 00:00:00 PDT 2018
15-Jul-2018
The format is yyyy-MM-dd. The String is 2018-07-15
package com.jackrutorial;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateExample2 {
public static void main(String[] args) throws ParseException {
String str = "2018-07-15";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = formatter.parse(str);
System.out.println(date);
System.out.println(formatter.format(date));
}
}
Output
Sun Jul 15 00:00:00 PDT 2018
2018-07-15
The format is yyyy-MM-dd HH:mm:ss. The String is 2018-07-15 18:00:00
package com.jackrutorial;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateExample3 {
public static void main(String[] args) throws ParseException {
String str = "2018-07-15 18:00:00";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = formatter.parse(str);
System.out.println(date);
System.out.println(formatter.format(date));
}
}
Output
Sun Jul 15 18:00:00 PDT 2018
2018-07-15 18:00:00
The format is dd/MM/yyyy. The String is 15/07/2018
package com.jackrutorial;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateExample4 {
public static void main(String[] args) throws ParseException {
String str = "15/07/2018";
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = formatter.parse(str);
System.out.println(date);
System.out.println(formatter.format(date));
}
}
Output
Sun Jul 15 00:00:00 PDT 2018
15/07/2018
The format is dd/MM/yyyy HH:mm:ss. The String is 15/07/2018 18:00:00
package com.jackrutorial;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateExample5 {
public static void main(String[] args) throws ParseException {
String str = "15/07/2018 18:00:00";
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = formatter.parse(str);
System.out.println(date);
System.out.println(formatter.format(date));
}
}
Output
Sun Jul 15 18:00:00 PDT 2018
15/07/2018 18:00:00
The format is E, MMM dd yyyy. The String is Sun, Jul 15 2018
package com.jackrutorial;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateExample6 {
public static void main(String[] args) throws ParseException {
String str = "Sun, Jul 15 2018";
SimpleDateFormat formatter = new SimpleDateFormat("E, MMM dd yyyy");
Date date = formatter.parse(str);
System.out.println(date);
System.out.println(formatter.format(date));
}
}
Output
Sun Jul 15 00:00:00 PDT 2018
Sun, Jul 15 2018
The format is E, MMM dd yyyy HH:mm:ss. The String is Sun, Jul 15 2018 18:00:00
package com.jackrutorial;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateExample7 {
public static void main(String[] args) throws ParseException {
String str = "Sun, Jul 15 2018 18:00:00";
SimpleDateFormat formatter = new SimpleDateFormat("E, MMM dd yyyy HH:mm:ss");
Date date = formatter.parse(str);
System.out.println(date);
System.out.println(formatter.format(date));
}
}
Output
Sun Jul 15 18:00:00 PDT 2018
Sun, Jul 15 2018 18:00:00
The format is EEEE, MMM dd, yyyy HH:mm:ss a. The String is Sun, Jun 15, 2018 06:00:00 PM
package com.jackrutorial;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateExample8 {
public static void main(String[] args) throws ParseException {
String str = "Sun, Jun 15, 2018 06:00:00 PM";
SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMM dd, yyyy HH:mm:ss a");
Date date = formatter.parse(str);
System.out.println(date);
System.out.println(formatter.format(date));
}
}
Output
Fri Jun 15 06:00:00 PDT 2018
Friday, Jun 15, 2018 06:00:00 AM