This is the second part of the article which I have started around date and String in Java. In the first part we have seen about converting a java.util.Date object into String format and in this part we will see the other way around. Means you have String in any date format e.g. yyyyMMdd 20110924 and you want to create a java.util.Date object from that. We will list steps to convert String into Date in Java and then we will see different examples with different date formats e.g. ddMMyy or dd-MM-yyyy.
Steps to Convert String into Date
Converting String to date is rather common scenario because you may get date in a String format from any file or xml document.
Here are steps we need to use for conversion in java:
1) Create a SimpleDateFormat object
2) Call parse() method of SimpleDateFormat and cast the result into Date object and you are done. parse() method of SimpleDateFormat throws ParseException so you need to either throw it or you can provide handling of this exception. Let’s see some examples of converting string to date in java to get hold of concept.
DateFormat formatter;
Date convertedDate;
// yyyyMMdd format e.g."20110914"
String yyyyMMdd = "20110914";
formatter = new SimpleDateFormat("yyyyMMdd");
convertedDate = (Date) formatter.parse(yyyyMMdd);
//convert string to date with ddMMyyyy format example "14092011"
String ddMMyyyy = "14092011";
formatter = new SimpleDateFormat("ddMMyyyy");
convertedDate = (Date) formatter.parse(yyyyMMdd);
//string into date in java with dd-MM-yyyy format e.g. "14-09-2011"
String dd_MM_YY = "14-09-2011";
formatter = new SimpleDateFormat("dd_MM_YYYY");
convertedDate = (Date) formatter.parse(yyyyMMdd);
// dd/MM/yyyy format for example "14/09/2011"
String stringDateFormat = "14/09/2011";
formatter = new SimpleDateFormat("dd/MM/yyyy");
convertedDate = (Date) formatter.parse(yyyyMMdd);
//parsing string into date with dd-MMM-yy format e.g. "14-Sep-11"
String dMMMyy = "14-Sep-11";
formatter = new SimpleDateFormat("yyyyMMdd");
convertedDate = (Date) formatter.parse(yyyyMMdd);
//convert string to date of dd-MMMM-yy format e.g. "14-September-11"
String dMMMMyy = "14-September-11";
formatter = new SimpleDateFormat("yyyyMMdd");
convertedDate = (Date) formatter.parse(yyyyMMdd);
You can check the java doc of DateFormat for all the symbols it supports and what is meaning for that but here I am listing some common points which is worth remembering while working with SimpleDateFormat for conversion.
1) Confusion between “m” and “M”, small case “m” represent minutes while “M” represent Month Also “d” represent date in month while “D” represent Day of week. This is most common cause of error while converting String to date and back date to string. In shot ddMMyy is not equal to DDmmyy.
2) SimpleDateFormat is not thread-safe. They are not synchronized so its better you create separate SimpleDateFormat for each thread to avoid any race condition while parsing.
Java is very rich in terms of date time support and it also provides convenient way to convert string to date which is very handy while working in java application.
Related Java tutorials
Do you like Books?
If you love to read books here are few Java titles which is worth of money :

3 comments:
Nice. Recently I stumbled upon this library: http://www.pojava.org/ Using this library you can easily handle different input date formats.
Or you can also use Joda time.
These other libraries are thread safe (unlike SimpleDateFormat).
Also in future Java, there will be a complete revamp of SimpleDateFormat (unfortunately I don't know if it will be Java 8 or some other more distant Java).
Thanks Jirka for this update, yes SimpleDateFormat needs a revamp at least on thread-safety issue, it doesn't make sense to have separate formatter for separate thread in my opinion.
Great Java SimpleDateFormat tutorial. I have read some of your java article and want to thank you for good work. I most like the last part of your article where you have pointed out subtle details on String to Date conversion
Post a Comment