6 Replies Latest reply: Jul 16, 2008 8:33 AM by 807589 RSS

    Converting String to Date

    807589
      Hi All,

      I am having a String value which is having date in the format 'Mon Apr 14 16:07:52 IST 2008'

      Now I want to convert this String to Date with format 'mm/dd/yyyy' , to display as 04/14/2008

      Also I want to add 5 days to this date and display the new date 04/19/2008

      I don't know How to convert the string which is having date in 'Mon Apr 14 16:07:52 IST 2008' format to format I need.


      Please help me.
        • 1. Re: Converting String to Date
          807589
          take a look at SimpleDateFormat
          • 2. Re: Converting String to Date
            PhHein
            SimpleDateFormat is the way to go as posted before, but the day (Mon) kills the parser. I have no idea why.
            This works though:
            String dateString = "Mon Apr 14 16:07:52 IST 2008";
            SimpleDateFormat sdf = new SimpleDateFormat("MMM dd hh:mm:ss ZZZ yyyy");
            Date date = sdf.parse(dateString.substring(4));
            From there on you can use Calendar to add days and another SimpleDateFormat to format the Date objects to your output pattern.
            • 3. Re: Converting String to Date
              807589
              As your string contain the name of Month in year, Day in week, these valuse may different in different locales.
              So you should select a locale to construct the SimpleDateFormat like below:
              String dateString = "Mon Apr 14 16:07:52 IST 2008";
              SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd hh:mm:ss ZZZ yyyy", Locale.US);
              Date date = sdf.parse(dateString);
              • 4. Re: Converting String to Date
                PhHein
                That's it, thanks.
                • 5. Re: Converting String to Date
                  807589
                  Reward the dukes to the person that helped you and mark the question closed.
                  • 6. Re: Converting String to Date
                    807589
                    Hi ,
                    I wrote a class that tries to parse a String Date and converts it to object. However, I found a very interesting thing while testing this code. I was trying to paarse "2008:03:04 16:21:05" string by using
                    DateFormat.getDateFromString(null,"2008:03:04 16:21:05" )

                    It should by pass all try loop accept the one with yyyy:MM:dd HH:mm:ss format. But it went inside the try loop which had MM:dd:yyyy HH:mm:ss format and gave me a wrong date object with year 0171, april 03.
                    Could any one help me to understand why it is happening? as a work around I put MM:dd:yyyy HH:mm:ss at the end of the program and it is giving me correct object as it did not have to go that far before matching the format.

                    Thanks,
                    AL-Amin



                    Code:




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

                    public class DateFormat
                    {


                    public Date getDateFromString(String format, String parseDate)
                    {
                         Date d;
                         if(format!=null) {
                    try
                    {
                    SimpleDateFormat sdf = new SimpleDateFormat(format);
                    d = sdf.parse(parseDate);
                    }
                    catch(Exception e)
                    {
                    System.out.println(e.toString());
                    d = null;
                    }
                    return d;
                    }
                         else {
                              try
                         {
                                   format = "yyyy-MM-dd HH mm ss";
                         SimpleDateFormat sdf = new SimpleDateFormat(format);
                         d = sdf.parse(parseDate);
                         }
                         catch(Exception e)
                         {
                         System.out.println(e.toString());
                         System.out.println("The Date was not of '"+format+" ' format");
                         d = null;
                         }
                         if(d==null) {
                              try
                              {
                                        format = "yyyy-MM-dd HH:mm:ss";
                              SimpleDateFormat sdf = new SimpleDateFormat(format);
                              d = sdf.parse(parseDate);
                              }
                              catch(Exception e)
                              {
                              System.out.println(e.toString());
                              System.out.println("The Date was not of '"+format+" ' format");
                              d = null;
                              }     
                         }
                         if(d==null) {
                              try
                              {
                                        format = "yyyy-MM-dd HH_mm_ss";
                              SimpleDateFormat sdf = new SimpleDateFormat(format);
                              d = sdf.parse(parseDate);
                              }
                              catch(Exception e)
                              {
                              System.out.println(e.toString());
                              System.out.println("The Date was not of '"+format+" ' format");
                              d = null;
                              }     
                         }
                         if(d==null) {
                              try
                              {
                                        format = "yyyy-MM-dd HH-mm-ss";
                              SimpleDateFormat sdf = new SimpleDateFormat(format);
                              d = sdf.parse(parseDate);
                              }
                              catch(Exception e)
                              {
                              System.out.println(e.toString());
                              System.out.println("The Date was not of '"+format+" ' format");
                              d = null;
                              }     
                         }
                         if(d==null) {
                              try
                              {
                                        format = "yyyy-MM-dd-HH-mm-ss";
                              SimpleDateFormat sdf = new SimpleDateFormat(format);
                              d = sdf.parse(parseDate);
                              }
                              catch(Exception e)
                              {
                              System.out.println(e.toString());
                              System.out.println("The Date was not of '"+format+" ' format");
                              d = null;
                              }     
                         }
                         if(d==null) {
                              try
                              {
                                        format = "yyyyMMddHHmmss";
                              SimpleDateFormat sdf = new SimpleDateFormat(format);
                              d = sdf.parse(parseDate);
                              }
                              catch(Exception e)
                              {
                              System.out.println(e.toString());
                              System.out.println("The Date was not of '"+format+" ' format");
                              d = null;
                              }     
                         }
                         if(d==null) {
                              try
                              {
                                        format = "MM-dd-yyyy HH:mm:ss";
                              SimpleDateFormat sdf = new SimpleDateFormat(format);
                              d = sdf.parse(parseDate);
                              }
                              catch(Exception e)
                              {
                              System.out.println(e.toString());
                              System.out.println("The Date was not of '"+format+" ' format");
                              d = null;
                              }     
                         }
                              if(d==null) {
                              try
                              {
                                        format = "dd-MM-yyyy HH:mm:ss";
                              SimpleDateFormat sdf = new SimpleDateFormat(format);
                              d = sdf.parse(parseDate);
                              }
                              catch(Exception e)
                              {
                              System.out.println(e.toString());
                              System.out.println("The Date was not of '"+format+" ' format");
                              d = null;
                              }     
                         }
                         if(d==null) {
                              try
                              {
                                        format = "MM:dd:yyyy HH:mm:ss";
                              SimpleDateFormat sdf = new SimpleDateFormat(format);
                              d = sdf.parse(parseDate);
                              }
                              catch(Exception e)
                              {
                              System.out.println(e.toString());
                              System.out.println("The Date was not of '"+format+" ' format");
                              d = null;
                              }     
                         }
                         if(d==null) {
                              try
                              {
                                        format = "dd:MM:yyyy HH:mm:ss";
                              SimpleDateFormat sdf = new SimpleDateFormat(format);
                              d = sdf.parse(parseDate);
                              }
                              catch(Exception e)
                              {
                              System.out.println(e.toString());
                              System.out.println("The Date was not of '"+format+" ' format");
                              d = null;
                              }     
                         }
                    if(d==null) {
                              try
                              {
                                        format = "yyyy:MM:dd HH:mm:ss";
                              SimpleDateFormat sdf = new SimpleDateFormat(format);
                              d = sdf.parse(parseDate);
                              }
                              catch(Exception e)
                              {
                              System.out.println(e.toString());
                              System.out.println("The Date was not of '"+format+" ' format");
                              d = null;
                              }     
                         }

                         if(d==null) {
                              System.err.println("None of the Date format worked. Please modify the calling method and provide a specific format");
                         }
                         return d;
                         
                         }
                    }
                    }