views:

63

answers:

1

Hi All,

Im having a problem with java date's, when i pass a date before 1949 into the bellow method. The date i have returned is for example 2049, im aware it has somthing to do with the date format and thought using yyyy instead of RRRR would have fixed it. But i just dont understand why or how to reslove it. Any help will be much apreciated

public static java.sql.Date parseDate(String date) throws ParseException {
  if (date != null) {
   SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
   return new java.sql.Date(dateFormat.parse(date).getTime());
  }
  return null;
 }

Thanks Jon

+2  A: 

let me format that for you..

public static java.sql.Date parseDate(String date) throws ParseException {
    if (date != null) { 
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy"); 
        return new java.sql.Date(dateFormat.parse(date).getTime()); 
    } 
    return null; 
}

This I suspect is what you want...

private Date convertDate() throws ParseException
{
    String dateStr = "21-02-2010";
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
    Date date = null;
    if (dateStr != null)
    {
      date = new Date(dateFormat.parse(dateStr).getTime());
    }

    System.out.println(date);
    return date;
}

For 21-02-2010 you will get... Sun Feb 21 00:00:00 GMT 2010

For 21-02-1938 you will get... Mon Feb 21 00:00:00 GMT 1938

Does that help? I might of been due to you having MMM in your code.

jeff porter