How to convert a formated date "2001/03/09" as String object that is "YYYY/MM/DD" into a java.util.Date object.
- Posted by: Farhan Alam
- Posted on: March 17 2001 12:10 EST
- How to convert String into java.util.Date by Rick Grashel on March 18 2001 13:22 EST
- How to convert String into java.util.Date by Prasath Balakrishnan on March 18 2001 16:40 EST
public class DateFormatTest
String dateString = "2001/03/09";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/mm/dd");
Date convertedDate = dateFormat.parse(dateString);
System.out.println("Converted string to date : " + convertedDate);
public static void main(String argv)
The Format String should be SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); Upper case MM represents month... lower case mm means minutes.
Thank you Stefan Avigue for the reply, I was really struggling for why the format is getting wrong.
I feel u can use the toString() directly after having obtained the date from the Collections package from the java.util.Date itself.
See if it works.
Have a look at:
It's not easy to convert String to date in multi-threading environment, because SimpleDateFormat is not thread-safe. See this tutorial to learn how we can use SimpleDateFormat safely to convert String to Date in Java.