4 Replies Latest reply on May 5, 2009 10:10 AM by 807588

    Problem in SimpleDateFormat

      **hi folks ,**
      **I am having a problem in SimpleDateFormat class. I have a method like this**

      public static Date getDateToString(Date dt,String pattern){ 

      Date outputDate=null;
      SimpleDateFormat formatter=new SimpleDateFormat(pattern);
      }catch(Exception e){ 

      return outputDate;

      public static void main(String args[]){ 

      System.out.print(" Date is === "+DateUtils.getDateToString((new Date()),"MM/dd/yyyy HH:mm") );

      running the code throws an exception like

      Unparseable date: "Tue May 05 13:38:42 IST 2009"
      at java.text.DateFormat.parse(Unknown Source)

      Can any one figure out what is the problem here in this code?
      and how can I convert the date to my desired format. I want to convert a Date into a Date with a specific format ("MM/dd/yyyy HH:mm").
        • 1. Re: Problem in SimpleDateFormat
          Date objects don't have a format. They only contain the number of milliseconds since 1/1/1970 UTC. The name of your method does not reflect it's function and though I can see what you are trying to do it is founded on a misunderstanding of java.util.Date
          • 2. Re: Problem in SimpleDateFormat
            No, you don't have a prolem with SimpleDateFormat.

            You have a serious problem in your understanding of what a Date is (from a computers point of view).

            A date is the number of milliseconds since Midnight Jan 1st, 1970 UTC. It is a number. It has no format. You use SimpleDateFormat to be able to parse a String to create this number, or to format a DateString representation of that number. You do not use it to convert a Date in one format to a date in another format because, as already said, a Date has no format.

            Edit: Damn! Too slow, again!
            • 3. Re: Problem in SimpleDateFormat
              Initially I was returning String thats why the method name is wrong.But I have changed my code and now I want to return a Date object.Ok I get that point..Date object doesn't accept format but is it possible the change the format by using SimpleDateFormat and in doing that should it always return a String not a Date object(as Date object does not have format)...??
              • 4. Re: Problem in SimpleDateFormat
                Thanks ...yes I that was a problem in my understanding.Now it is clear.