开发者

How to convert date saved in String format to Date format [duplicate]

开发者 https://www.devze.com 2023-01-04 22:43 出处:网络
This question already has answers here: Closed 10 years ago. Possible Duplicate: DateFormat conversion problem in java?
This question already has answers here: Closed 10 years ago.

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


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;
}


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消