5 Replies Latest reply: May 14, 2010 4:30 AM by 807580 RSS

    String to SimpleDateFormat Conversion

    807580
      I have a String in YYYYMMDDHHMMSS format. I want to convert it into SimpleDateFormat.
      Please share your ideas.......

      Thanks in advance
        • 1. Re: String to SimpleDateFormat Conversion
          807580
          What? Are you saying that you have a string that's exactly "YYYYMMDDHHMMSS", and you want to use it as a date format? Then just use the SimpleDateFormat constructor that takes a String as an argument.

          If you're saying that you have that string, but you want to know if that's the correct format to use...then you should read the SimpleDateFormat docs to see if it will do what you want it to do. (My guess is, no -- is "MM" supposed to mean months or minutes depending on where it is?)

          If you're saying that you have a string expressing a date and you want to turn it into a date format...then that doesn't make any sense.
          • 2. Re: String to SimpleDateFormat Conversion
            807580
            Hi

            If you know the format of input string, you can try below code...

            import java.text.ParseException;
            import java.text.SimpleDateFormat;
            import java.util.Date;

            public class DateFormatTest
            {
                 public static void main(String [] args) throws ParseException
                 {
                      String inputDate = "20100514101001";

                      SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
                      
                      Date myDate = df.parse(inputDate);
                      
                      SimpleDateFormat dfNext = new SimpleDateFormat("dd/MM/yyyy hh mm ss");
                      
                      
                      System.out.println("Formatted Date: " + dfNext.format(myDate));
                      
                 }
            }
            • 3. Re: String to SimpleDateFormat Conversion
              807580
              I think MM is for Month. mm for minutes.
              • 4. Re: String to SimpleDateFormat Conversion
                807580
                Thank you Paul for ur input.

                Actually the input is a String from LDAP.
                while showing it on the JSP it was not properly formatted.
                I tried the following code,
                SimpleDateFormat formatter;
                formatter = new SimpleDateFormat("yyyy.MM.dd  'at' HH:mm:ss ");
                lastDate = formatter.format(lastLoginDateTime);

                Its working.....
                • 5. Re: String to SimpleDateFormat Conversion
                  807580
                  Bimal,

                  Your code is exactly my requirement.
                  Let me check it with my app.

                  Thanks alot.