5 Replies Latest reply: Aug 5, 2007 4:29 PM by 807605 RSS

    problem in parseing string to date obj

    807605
      import java.util.*;
      import java.text.*;

      class timeStamp
      {
           public static void main(String[] args)
           {
                try{
                java.util.Date d = new java.util.Date(); //null;//new java.util.Date();

                System.out.println(d);
                String myDate = "23-5-2007 23:24:25.999";

                SimpleDateFormat format1= new SimpleDateFormat("dd-MM-yyyy hh:mm:ss.SSS");
                //ParsePosition pos = new ParsePosition(0);
                d= format1.parse(myDate);
                System.out.println(d);


                }catch(ParseException pe)
                {
                     pe.printStackTrace();

                }
           }
      }
      /*
      output:

      Wed May 23 23:24:25 GMT+05:30 2007


      but the required format is 23-5-2007 23:24:25.999

      */
        • 1. Re: problem in parseing string to date obj
          796254
          This code gave the correct behavior for me:
          package cruft;
          
          import java.text.DateFormat;
          import java.text.ParseException;
          import java.text.SimpleDateFormat;
          import java.util.Date;
          
          class DateFormatTester
          {
             private static final String DEFAULT_DATE_PATTERN = "dd-MM-yyyy hh:mm:ss.SSS";
             public static final DateFormat DEFAULT_FORMAT = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
          
             public static void main(String[] args)
             {
                try
                {
                   System.out.println(new Date());
          
                   String myDate = "23-5-2007 23:24:25.999";
                   System.out.println(DEFAULT_FORMAT.parse(myDate));
                }
                catch (ParseException pe)
                {
                   pe.printStackTrace();
                }
             }
          }
          Output:

          Sun Aug 05 09:27:32 EDT 2007
          Wed May 23 23:24:25 EDT 2007

          Process finished with exit code 0

          %
          • 2. Re: problem in parseing string to date obj
            807605
            output required by is
            as mention
            23-5-2007 23:24:25.999
            not which was printed
            Wed May 23 23:24:25 EDT 2007

            swaps2303
            swaps2303
            • 3. Re: problem in parseing string to date obj
              807605
              what did you expect the result to be? All of your formatting information is in your SimpleDateFormat object, format1. The Date object, d, will not magically receive this formatting info. Why not use your SimpleDateFormat object to also format your output?
              • 4. Re: problem in parseing string to date obj
                807605
                client provides a string which sud be converted to Date object
                so dat data cud be fetch from database

                so i want to convert it into date object
                by using format() it will convert it into string

                i dont want the day to be the part of output
                the out put sud be in
                dd-mm-yyyy HH:MM:ss.sss format

                Message was edited by:
                swaps2303
                • 5. Re: problem in parseing string to date obj
                  807605
                  How about plugging whatever date result you have into your SimpleDateFormat object via the format method. The API is here:

                  http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html#format