How to parse Date from String and format Date as String
- Details
- Written by Nam Ha Minh
- Last Updated on 02 July 2019   |   Print Email
In this post, I will guide you how to convert String to Date and format Date to String in Java.
It’s a very common case in which one has a need to create a Date object from a given String in a specified date pattern, for example:
String strDate = “25/03/1990”;
Here, the strDate string is in the format “dd/MM/yyyy” which is needed to be parsed to create a new Date object with the exact components (day, month and year). This is referred as date parsing.
One also needs to display a Date object as a String in a specified date pattern. This is referred as date formatting.
In Java, we can accomplish that by using the following two classes in the java.text package:
- DateFormat: an abstract class for date/time formatting.
- SimpleDateFormat: a concrete sub class of the DateFormat class. It implements the parsing and formatting dates in a locale-sensitive manner.
For example, the following code snippet tries to parse a String to a Date object with the date pattern “dd/MM/yyyy”:
String pattern = "dd/MM/yyyy"; DateFormat dateFormat = new SimpleDateFormat(pattern); String strDate = "25/03/1990"; try { Date parsedDate = dateFormat.parse(strDate); System.out.println("The parsed date is: " + parsedDate); } catch (ParseException e) { e.printStackTrace(); }
Output:
The parsed date is: Sun Mar 25 00:00:00 ICT 1990
And the following code tries to format a Date object as a String in the pattern “dd/MM/yyyy”:
String pattern = "dd/MM/yyyy"; DateFormat dateFormat = new SimpleDateFormat(pattern); Date newDate = new Date(); String formattedDate = dateFormat.format(newDate); System.out.println("The formatted date is: " + formattedDate);
Output:
The formatted date is: 26/03/2014
The above examples parse and format dates according to a fixed pattern. In case you want to parse and format dates according to user’s default locale or a specific locale, the following SimpleDateFormat’s factory methods would help:
- DateFormat.getDateInstance(): returns a date formatter with the default formatting style for the current locale.
- DateFormat.getDateInstance(int style, Locale aLocale): returns a date formatter with the given formatting style and the given locale.
Here’s an example of date formatting and parsing for the default locale:
DateFormat dateFormater = DateFormat.getInstance(); String formattedDate = dateFormater.format(new Date()); System.out.println("The formatted date is: " + formattedDate); try { Date parsedDate = dateFormater.parse("3/31/14 12:00 AM"); System.out.println("The parsed date is: " + parsedDate); } catch (ParseException ex) { ex.printStackTrace(); }
Output:
The formatted date is: 3/27/14 11:43 AM The parsed date is: Mon Mar 31 00:00:00 ICT 2014
Here’s an example of date formatting and parsing for French locale:
DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.FRANCE); String formattedDate = dateFormatter.format(new Date()); System.out.println("The formatted date is: " + formattedDate); try { Date parsedDate = dateFormatter.parse("01 avril 2014"); System.out.println("The parsed date is: " + parsedDate); } catch (ParseException ex) { ex.printStackTrace(); }
Output:
The formatted date is: 27 mars 2014 The parsed date is: Tue Apr 01 00:00:00 ICT 2014
NOTE: When using the factory methods, we can set the date pattern as follows:
DateFormat dateFormat = DateFormat.getInstance(); SimpleDateFormat simpleDateFormatter = (SimpleDateFormat) dateFormat; simpleDateFormatter.applyPattern("dd/MM/yyyy");
Then parse the date as same as previous code:
try { Date parsedDate = simpleDateFormatter.parse("27/03/1990"); System.out.println("The parsed date is: " + parsedDate); } catch (ParseException ex) { ex.printStackTrace(); }
This is the recommended way of using the date formatter in Java.
References:
Other Java Coding Tutorials:
- 10 Common Mistakes Every Beginner Java Programmer Makes
- 10 Java Core Best Practices Every Java Programmer Should Know
- How to become a good programmer? 13 tasks you should practice now
- How to calculate MD5 and SHA hash values in Java
- How to generate random numbers in Java
- Java File Encryption and Decryption Example
Comments