4 Replies Latest reply: Jan 31, 2007 3:36 AM by 807607 RSS

    Facing Problem in parsing a string to date

    807607
      Hi,
      I was trying to change a string into date with date format ("EEEE,MMM,d,h:mm") but I always get the year as 1970.
      here is my code
      String strDate="Saturday,Jan 19 7:31";
      String dateFormat3="EEEE,MMM,d,h:mm";
           try {
           DateFormat myDateFormat = new SimpleDateFormat(dateFormat3);
           result1=myDateFormat.parse(strDate);
      }
      catch(ParseException pe) {
                  System.out.println("ERROR: could not parse date in string \"" +
                       "\"");
              }
      any solution for it.
        • 1. Re: Facing Problem in parsing a string to date
          807607
          what do you expect? There is no year in your date. If you want it to default to the current date you're going to have to do that yourself.

          Ted.
          • 2. Re: Facing Problem in parsing a string to date
            807607
            This is my actual code
            import java.text.DateFormat;
            import java.text.ParseException;
            import java.text.SimpleDateFormat;
            import java.util.Date;
            import java.util.Locale;
            
            public class TestingDate {
            
                 /**
                  * @param args
                  */
                 public static void main(String[] args) {
                      // TODO Auto-generated method stub
                      
                      
                      
                      String dateFormat="EEEE, MMM d h:mm a";
                      
                      Date test=new Date(2007,0,19, 19, 31);
                      System.out.println(" original date is "+test);
                                
                      String stringResult=DateToString(test,dateFormat);
                      System.out.println("Date to string is "+stringResult);
                      
                      Date dateResult=stringToDate(stringResult,dateFormat);
                      System.out.println(" String to date is "+dateResult);
                      
                      String stringResult2=DateToString(dateResult,dateFormat);
                      System.out.println(" Date to string  is "+stringResult2);
            
                 }
            public static String DateToString(Date test, String dateFormat) {
                     
                     String result = null;
                     try {
                          DateFormat myDateFormat = new SimpleDateFormat(dateFormat);
                             result = myDateFormat.format(test);
                             //System.out.println(" reslut date is "+result);
                      } catch (Exception e) {
                           System.out.println(" Exception is "+e);
                      }
            
                      return result;
                }
            public static Date stringToDate(String strDate,String dateFormat1){
                 
                 
                 Date result1=null;
                 
                 try {
                      
                      DateFormat myDateFormat = new SimpleDateFormat(dateFormat1);
                      result1=myDateFormat.parse(strDate);
                      
                 } 
                 catch(Exception e){
                      System.out.println(" exception is "+e);
                 }
                 return result1;
            }
            
            }
            I am facing problem in getting the actual date. Please suggest the solution.
            • 3. Re: Facing Problem in parsing a string to date
              807607
              Date test=new Date(2007,0,19, 19, 31);
              Why do you set your month to 0?
              The reason why you loose your year is because you don't include it in your DateFormat pattern. You first print it without year and then you parse it again. When you parse the date without year it initialises the year to 1970. Dates are actually represented internally as the number of milliseconds before or after 1970/1/1.
              • 4. Re: Facing Problem in parsing a string to date
                807607
                Ya i got the answer,
                Thanks for help