java iteratorHandling text, dates, numbers and messages is the essential part of every program or a project in any programming language. Java provides a ‘java.text’ package with implemented classes for these purposes. One of the classes is ‘java.text.SimpleDateFormat’, which is a concrete class (implement all methods of ‘DateFormat’ abstract class) for formatting and parsing dates in a locale-sensitive manner. It allows formatting of Date objects to a Java String, parsing Java String to Date object and normalization.

What is Java String to Date Conversion?

Converting a Java String to Date, using an instance method ‘parse’ of class ‘SimpleDateFormat’, is called string to date parsing. ‘SimpleDateFormat’ allows any user defined date pattern or format for date-time formatting and convert a particular String which follows that pattern into Date object.

To learn more about Java Strings, take a course at Udemy.com.

How to Convert Java String to a Date Object?

Before the implementation of Java String to Date Object, the following are some of the important points that must be kept in mind:

Code snippet:

publicstaticvoid main (String args[]) throws ParseException{

SimpleDateFormat dateFormatter = new SimpleDateFormat ( "dd-MMM-yyyy" );

String dateInString = "12-May-2014";

try {

java.util.Date dateObject = dateFormatter.parse ( dateInString );

System.out.println( dateObject );

System.out.println( dateFormatter.format ( dateObject ) );

} catch ( ParseException e) {

System.out.println( e );

};

}

Output:

Mon May 12 00:00:00 PKT 2014

12-May-2014

In the above code snippet, a Java String to Date conversion is achieved using ‘SimpleDateFormat’ and ‘java.util.Date’ classes. To start, a valid date format is created with a ‘SimpleDateFormat’’ object. Then ‘parse’, an instance method of ‘SimpleDateFormat’ with a String object as an argument returns an object of a Java Date Object. Next, that Java Date object is again converted to a String object using another instance method ‘format’ of ‘SimpleDateFormat’ with a Java Date object as argument.

Interested in learning more about Java? Take a course at Udemy.com.

Some of the common date and time patterns in ‘SimpleDateFormat’ for Java String to Date conversion are:

The following code snippet demonstrates the basic use of above pattern letters.

Code snippet:

DateFormat dateFormatter = new SimpleDateFormat ( "dd/MM/yyyy" );

String dateInString = "12/05/2014";

DateFormat dateFormatter_2 = new SimpleDateFormat ( "dd-MM-yyyy HH:mm:ss" );

String dateInString_2 = "12-05-2014 22:37:50";

DateFormat dateFormatter_3 = new SimpleDateFormat ( "dd-MMM-yyyy" );

String dateInString_3 = "12-May-2014";

DateFormat dateFormatter_4 = new SimpleDateFormat ( "MM dd, yyyy" );

String dateInString_4 = "05 12, 2014";

DateFormat dateFormatter_5 = new SimpleDateFormat ( "E, MMM dd yyyy" );

String dateInString_5 = "Mon, May 12 2014";

DateFormat dateFormatter_6 = new SimpleDateFormat ( "E, MMM dd yyyy HH:mm:ss" );

String dateInString_6 = "Mon, May 12 2014 23:37:50";

DateFormat dateFormatter_7 = new SimpleDateFormat ( "dd-MMMM-yy" );

String dateInString_7 = "14-September-14";

try {

java.util.Date dateObject = dateFormatter.parse ( dateInString );

System.out.println ( dateObject );

System.out.println ( dateFormatter.format ( dateObject ) );

dateObject = dateFormatter_2.parse  (dateInString_2 );

System.out.println ( dateObject );

System.out.println ( dateFormatter_2.format ( dateObject ) );

dateObject = dateFormatter_3.parse ( dateInString_3 );

System.out.println ( dateObject );

System.out.println ( dateFormatter_3.format ( dateObject ) );

dateObject = dateFormatter_4.parse ( dateInString_4 );

System.out.println ( dateObject );

System.out.println ( dateFormatter_4.format ( dateObject ) );

dateObject = dateFormatter_5.parse ( dateInString_5 );

System.out.println ( dateObject );

System.out.println ( dateFormatter_5.format ( dateObject ) );

dateObject = dateFormatter_6.parse ( dateInString_6 );

System.out.println ( dateObject );

System.out.println ( dateFormatter_6.format ( dateObject ) );

dateObject = dateFormatter_7.parse ( dateInString_7 );

System.out.println ( dateObject );

System.out.println ( dateFormatter_7.format ( dateObject ) );

} catch ( ParseException e) {

System.out.println ( e );

};

Output:

Mon May 12 00:00:00 PKT 2014

12/05/2014

Mon May 12 22:37:50 PKT 2014

12-05-2014 22:37:50

Mon May 12 00:00:00 PKT 2014

12-May-2014

Mon May 12 00:00:00 PKT 2014

05 12, 2014

Mon May 12 00:00:00 PKT 2014

Mon, May 12 2014

Mon May 12 23:37:50 PKT 2014

Mon, May 12 2014 23:37:50

Sun Sep 14 00:00:00 PKT 2014

14-September-14

In the above code snippets multiple variations of Java String to Date object conversions with different pattern of Date formats have been demonstrated. The substring ‘MMM’ of format represents the first three characters of Name of Month while ‘MMMM’ represents the full Name of a Month. The ‘yy’ represents the last two digits of the Year.

Common Errors in Java String to Date Conversion

Following are the common errors that have been faced by about every programmer when converting the Java String to Date object.

java.util.Date dateObject = dateFormatter.parse ( dateInString );

 

Code snippet:

public class ThreadSafeSimpleDateFormat {

 

private DateFormat dateFormat;

public ThreadSafeSimpleDateFormat(String format) {

this.dateFormat = new SimpleDateFormat(format);

}

public synchronized String format(Date date) {

return dateFormat.format(date);

}

public synchronized Date parse(String string) throws ParseException {

return dateFormat.parse(string);

}

}

For learning more Java, take a course at Udemy.com.

Top courses in Java

Core Java Made Easy (Covers the latest Java 14)
Bharath Thippireddy
4.5 (1,854)
Java Programming Masterclass for Software Developers
Tim Buchalka, Tim Buchalka's Learn Programming Academy, Goran Lochert
4.6 (119,042)
Bestseller
The Complete Android & Java Developer Course - Build 21 Apps
Paulo Dichone | Android, Java, Flutter Developer and Teacher, Fahd Sheraz
4.2 (5,938)
Java Programming for Complete Beginners
in28Minutes Official
4.4 (9,907)

More Java Courses

Java students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.

Request a demo