Getting Started with Java String to Date Conversion

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:

  • The ‘SimpleDateFormat’ is not thread safe, which means one instance of this class cannot be used in multiple threads that involves performing the Java String to Date conversion and formatting Date objects.
  • The ‘parse’ method of ‘SimpleDateFormat’ throws ‘ParseException’ exception, when the defined date pattern is invalid.
  • In Java documentation it is advised to create a date-time formatting with either of ‘getTimeInstance’, ‘getDateInstance’, or ‘getDateTimeInstance’ in ‘DateFormat’. Each of these class methods can return a date-time formatting initialized with a default format pattern.

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:

  • Letter ‘y’ represents Year.
  • Letter ‘M’ represents Month in Year.
  • Letter ‘D’ represents Day in Month and ‘E’ is Day Name in Week.
  • Letter ‘a’ represents AM/PM marker and ‘h’ is Hour in AM/PM.
  • Letter ‘H’ represents Hour in a Day, ‘m’ represents Minute in Hour and‘s’ is Second in Minute.

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.

  • Most of the times a program throws an exception ‘ClassCastException’. This is because of the casting of ‘java.util.Date’ type Date object to ‘java.sql.Date’ type Date object. In a conversion of Java String to Date object, the remedy to this exception is to import a ‘java.util.Date’ class and remove the import of ‘java.sql.Date’ or use a complete name of a class ‘java.util.Date’ instead of ‘Date’ like

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

  • Another common error is ‘ParseException’ exception. Conversion of Java String to Date process throws this exception when the format of Date defines in ‘SimpleDateFormat’ is do not match with the string that has been passed to  ‘parse’ method.
  • Currently, most of the applications use multiple threads to fasten process execution. As ‘SimpleDateFormat’ is not a thread safe, therefore many issues may arise when it is accessed by multiple threads at same time. Remedy to this problem is to use modified ‘SimpleDateFormat’ class in Java String to Date conversion. Following code demonstrates the implementation of modified class (ThreadSafeSimpleDateFormat).

 

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.