In this article, we will learn how to convert String to Date in Java using different date formats
Q) What is the need of converting String to Date ?
- Generally, whenever we receive any data from web application then it is passed in the form of String only
- But for further processing we need to convert String to Date
- This article explains about String to Date conversion in different formats
- Note: Likewise, sometimes Date to String conversion is also required for reverse process
1. String to Date conversion in different formats:
We will use below formats to convert String to Date
- dd/MM/yyyy
- dd-MM-yyyy
- dd.MM.yyyy
- dd MM yyyy
- MM dd, yyyy
- dd-MMM-yyyy
- E, MMM dd yyyy
- E, MMM dd yyyy HH:mm:ss
- E, MMM dd yyyy hh:mm:ss a
Note: for creating above formats we need mainly 2 classes
2. String to Date conversion examples :
2.1 Convert String to Date in (dd/MM/yyyy) format :
StringToDateFormat1.java
package in.bench.resources.date.conversion;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateFormat1 {
public static void main(String[] args) throws ParseException {
// date in String
String strDate = "23/04/2021";
// formatter
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
// parse date in String format using above formatter
Date date = dateFormat.parse(strDate);
// print to console
System.out.println("Original String :- " + strDate);
System.out.println("\nConverted Date after formatting :- " + date);
}
}
Output:
Original String :- 23/04/2021
Converted Date after formatting :- Fri Apr 23 00:00:00 IST 2021
2.2 Convert String to Date in (dd-MM-yyyy) format :
StringToDateFormat2.java
package in.bench.resources.date.conversion;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateFormat2 {
public static void main(String[] args) throws ParseException {
// date in String
String strDate = "23-04-2021";
// formatter
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
// parse date in String format using above formatter
Date date = dateFormat.parse(strDate);
// print to console
System.out.println("Original String :- " + strDate);
System.out.println("\nConverted Date after formatting :- " + date);
}
}
Output:
Original String :- 23-04-2021
Converted Date after formatting :- Fri Apr 23 00:00:00 IST 2021
2.3 Convert String to Date in (dd MM yyyy) format :
StringToDateFormat3.java
package in.bench.resources.date.conversion;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateFormat3 {
public static void main(String[] args) throws ParseException {
// date in String
String strDate = "23 04 2021";
// formatter
DateFormat dateFormat = new SimpleDateFormat("dd MM yyyy");
// parse date in String format using above formatter
Date date = dateFormat.parse(strDate);
// print to console
System.out.println("Original String :- " + strDate);
System.out.println("\nConverted Date after formatting :- " + date);
}
}
Output:
Original String :- 23 04 2021
Converted Date after formatting :- Fri Apr 23 00:00:00 IST 2021
2.4 Convert String to Date in (dd.MM.yyyy) format :
StringToDateFormat4.java
package in.bench.resources.date.conversion;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateFormat4 {
public static void main(String[] args) throws ParseException {
// date in String
String strDate = "23.04.2021";
// formatter
DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
// parse date in String format using above formatter
Date date = dateFormat.parse(strDate);
// print to console
System.out.println("Original String :- " + strDate);
System.out.println("\nConverted Date after formatting :- " + date);
}
}
Output:
Original String :- 23.04.2021
Converted Date after formatting :- Fri Apr 23 00:00:00 IST 2021
2.5 Convert String to Date in (MM dd, yyyy) format :
StringToDateFormat5.java
package in.bench.resources.date.conversion;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateFormat5 {
public static void main(String[] args) throws ParseException {
// date in String
String strDate = "04 23, 2021";
// formatter
DateFormat dateFormat = new SimpleDateFormat("MM dd, yyyy");
// parse date in String format using above formatter
Date date = dateFormat.parse(strDate);
// print to console
System.out.println("Original String :- " + strDate);
System.out.println("\nConverted Date after formatting :- " + date);
}
}
Output:
Original String :- 04 23, 2021
Converted Date after formatting :- Fri Apr 23 00:00:00 IST 2021
2.6 Convert String to Date in (dd-MMM-yyyy) format :
StringToDateFormat6.java
package in.bench.resources.date.conversion;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateFormat6 {
public static void main(String[] args) throws ParseException {
// date in String
String strDate = "23-Apr-2021";
// formatter
DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
// parse date in String format using above formatter
Date date = dateFormat.parse(strDate);
// print to console
System.out.println("Original String :- " + strDate);
System.out.println("\nConverted Date after formatting :- " + date);
}
}
Output:
Original String :- 23-Apr-2021
Converted Date after formatting :- Fri Apr 23 00:00:00 IST 2021
2.7 Convert String to Date in (E, MMM dd yyyy) format :
StringToDateFormat7.java
package in.bench.resources.date.conversion;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateFormat7 {
public static void main(String[] args) throws ParseException {
// date in String
String strDate = "Fri, Apr 23 2021";
// formatter
DateFormat dateFormat = new SimpleDateFormat("E, MMM dd yyyy");
// parse date in String format using above formatter
Date date = dateFormat.parse(strDate);
// print to console
System.out.println("Original String :- " + strDate);
System.out.println("\nConverted Date after formatting :- " + date);
}
}
Output:
Original String :- Fri, Apr 23 2021
Converted Date after formatting :- Fri Apr 23 00:00:00 IST 2021
2.8 Convert String to Date in (E, MMM dd yyyy HH:mm:ss) format :
StringToDateFormat8.java
package in.bench.resources.date.conversion;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateFormat8 {
public static void main(String[] args) throws ParseException {
// date in String
String strDate = "Fri, Apr 23 2021 14:20:27";
// formatter
DateFormat dateFormat = new SimpleDateFormat("E, MMM dd yyyy HH:mm:ss");
// parse date in String format using above formatter
Date date = dateFormat.parse(strDate);
// print to console
System.out.println("Original String :- " + strDate);
System.out.println("\nConverted Date after formatting :- " + date);
}
}
Output:
Original String :- Fri, Apr 23 2021 14:20:27
Converted Date after formatting :- Fri Apr 23 14:20:27 IST 2021
2.9 Convert String to Date in (E, MMM dd yyyy hh:mm:ss a) format :
StringToDateFormat9.java
package in.bench.resources.date.conversion;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateFormat9 {
public static void main(String[] args) throws ParseException {
// date in String
String strDate = "Fri, Apr 23 2021 02:21:59 PM";
// formatter
DateFormat dateFormat = new SimpleDateFormat("E, MMM dd yyyy hh:mm:ss a");
// parse date in String format using above formatter
Date date = dateFormat.parse(strDate);
// print to console
System.out.println("Original String :- " + strDate);
System.out.println("\nConverted Date after formatting :- " + date);
}
}
Output:
Original String :- Fri, Apr 23 2021 02:21:59 PM
Converted Date after formatting :- Fri Apr 23 14:21:59 IST 2021
Hope, everyone found this article useful while converting date in String to Date format. If any edge cases needs to be covered then comment
Related Articles:
- Java 8 – How to convert java.util.Date to LocalDate and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalTime and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDateTime and vice-versa ?
- Java 8 – How to convert java.util.Date to ZonedDateTime and vice-versa ?
- Java 8 – How to convert java.util.Date to OffsetDateTime and vice-versa ?
- Java 8 – How to convert java.util.Date to Instant and vice-versa ?
- Java 8 – How to convert java.util.Date to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert java.util.Date to Calendar and vice-versa ?
- Java 8 – How to convert java.util.Date to GregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to XMLGregorianCalendar and vice-versa ?
- Java 9 – How to convert java.util.Date to LocalDate using ofInstant() method ?
- Java 9 – How to convert java.util.Date to LocalTime using ofInstant() method ?
- Java – How to convert java.util.Date to String in different formats ?
- Java – How to convert String to java.util.Date in different formats ?
- Java – How to get Date/time with AM/PM marker and Zone ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
Happy Coding !!
Happy Learning !!