Discussions

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

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

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

    fa.

    Threaded Messages (5)

  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.

    Prasath
  6. Have a look at:

    http://developer.java.sun.com/developer/qow/archive/120/index.html

    cheers,