Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to Convert String to Date in Java


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.



Check Here For How To Convert date To String In JAVA



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.






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.

http://codesforprogrammers.blogspot.com/feeds/posts/default?alt=rss


This post first appeared on Codes For Programmers(C4P), please read the originial post: here

Share the post

How to Convert String to Date in Java

×

Subscribe to Codes For Programmers(c4p)

Get updates delivered right to your inbox!

Thank you for your subscription

×