How to convert String into java.util.Date


Web tier: servlets, JSP, Web frameworks: How to convert String into java.util.Date

  1. How to convert String into java.util.Date (6 messages)

    How to convert a formated date "2001/03/09" as String object that is "YYYY/MM/DD" into a java.util.Date object.


    Threaded Messages (6)

  2. import java.text.*;
    import java.util.*;

    public class DateFormatTest
      public 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)
        new DateFormatTest();

  3. MM not mm[ Go to top ]

    The Format String should be SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); Upper case MM represents month... lower case mm means minutes.
  4. Re: MM not mm[ Go to top ]

    Thank you Stefan Avigue for the reply, I was really struggling for why the format is getting wrong.
  5. 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.

  6. Have a look at:

  7. Remember that SDF is not thread-safe[ Go to top ]

    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.