0

Possible Duplicate:
DateFormat conversion problem in java?

I am trying to convert string to date, but getting error.

I am getting date using :

URL xmlUrl = new URL(path);
URLConnection urlconn = xmlUrl.openConnection();
Date =  new Date(urlconn.getLastModified());

and then I ma saving this date in a file , which saves in the following format :

Mon Jun 21 16:31:24 Asia/Karachi 2010

and then when later I read this date from file as a String, I again want to save it to a Date, but I am getting error.

I tried :

DateFormat format = DateFormat.getDateInstance();
date =  format.parse(fileDate);

but I am getting error :

java.text.ParseException: Unparseable date: Mon Jun 21 16:31:24 Asia/Karachi 2010

Is there any way i can retrieve back the date.

Thanks

Community
  • 1
  • 1
Adil Bhatty
  • 17,190
  • 34
  • 81
  • 118
  • I had this problem already. check this [reto meier's answer](http://stackoverflow.com/questions/2600581/dateformat-conversion-problem-in-java/2600648#2600648) posted to me. hope it helps. – Praveen Jun 29 '10 at 13:51

2 Answers2

6
public String getconvertdate1(String date)
{
    DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    inputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    DateFormat outputFormat = new SimpleDateFormat("dd MMM yyyy");
    Date parsed = new Date();
    try
    {
        parsed = inputFormat.parse(date);
    }
    catch (ParseException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String outputText = outputFormat.format(parsed);
    return outputText;
}
Arun
  • 2,800
  • 23
  • 13
4

Try this. Have to specify the correct date format.

SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date d = format.parse(fileDate);
Robby Pond
  • 73,164
  • 16
  • 126
  • 119