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:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments