3 Replies Latest reply on Jul 9, 2007 4:23 PM by 807605

    String to timestamp conversion

    807605
      I have a string of the format yyyyMMdd:hh:mm:ss.SSSSS.
      I need to convert it to a timestamp value of
      the format dd-MMM-yy hh.mm.ss.SSSSS.

      I tried the following
      SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd:hh:mm:ss.SSSSS");
      java.util.Date date = sdf.parse(stdate);
      java.sql.Timestamp t = new java.sql.Timestamp(date.getTime());

      but it didnt work.

      Any help will be greatly appreciated.
        • 1. Re: String to timestamp conversion
          800282
                  final String INPUT = "yyyyMMdd:hh:mm:ss.SSSSS";
                  final String OUTPUT = "dd-MMM-yy hh.mm.ss.SSSSS";
                  String in = "20070709:18:01:23.0456";
                  java.util.Date date = (new SimpleDateFormat(INPUT)).parse(in);
                  String out = (new SimpleDateFormat(OUTPUT)).format(date);
                  System.out.println(out);
          • 2. Re: String to timestamp conversion
            800323
            I have a string of the format
            yyyyMMdd:hh:mm:ss.SSSSS.
            need to convert it to a timestamp value of
            he format dd-MMM-yy hh.mm.ss.SSSSS.

            I tried the following
            SimpleDateFormat sdf = new
            SimpleDateFormat("yyyyMMdd:hh:mm:ss.SSSSS");
            java.util.Date date = sdf.parse(stdate);
            java.sql.Timestamp t = new
            java.sql.Timestamp(date.getTime());

            but it didnt work.

            Any help will be greatly appreciated.
            I have a string of the format
            yyyyMMdd:hh:mm:ss.SSSSS.
            need to convert it to a timestamp value of
            he format dd-MMM-yy hh.mm.ss.SSSSS.
            Timestamps do not have a format - they are just a point in time. The toString() method will return a String representing the Timestamp in some format but this is not the same as having a format.
            I tried the following
            SimpleDateFormat sdf = new
            SimpleDateFormat("yyyyMMdd:hh:mm:ss.SSSSS");
            java.util.Date date = sdf.parse(stdate);
            java.sql.Timestamp t = new
            java.sql.Timestamp(date.getTime());

            but it didnt work.
            What do you mean by "didn't work"?
            • 3. Re: String to timestamp conversion
              807605
              Thanks a lot! Your solution worked.