如何转换日期保存字符串格式的日期格式 [英] How to convert date saved in String format to Date format

查看:140
本文介绍了如何转换日期保存字符串格式的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  在java中日期格式转换的问题?


我想字符串转换为日期,但收到错误。

我使用越来越日期:

  URL = XMLURL新的URL(路径);
URLConnection的urlconn = xmlUrl.openConnection();
日期=新的日期(urlconn.getLastModified());

然后我是个节省这个日期的文件,该文件保存在以下格式:

 周一6月21日16时31分24秒亚洲/卡拉奇2010

,然后当以后我从文件中读取一个字符串这一天,我再次想将其保存到一个日期,但我得到的错误。

我试过:

 日期格式FORMAT = DateFormat.getDateInstance();
日期= format.parse(FILEDATE);

但我得到的错误:

  java.text.ParseException:无法解析日期:星期一6月21日16点31分24秒亚洲/卡拉奇2010

有什么办法,我可以检索回来的日期。

感谢


解决方案

 公共字符串getconvertdate1(字符串日期)
{
    日期格式的InputFormat =新的SimpleDateFormat(YYYY-MM-DD HH:MM:SS);
    inputFormat.setTimeZone(TimeZone.getTimeZone(UTC));
    DateFormat的OUTPUTFORMAT =新的SimpleDateFormat(DD MMM YYYY);
    解析日期=新的日期();
    尝试
    {
        解析= inputFormat.parse(日期);
    }
    赶上(ParseException的E)
    {
        // TODO自动生成catch块
        e.printStackTrace();
    }
    字符串的outputText = outputFormat.format(解析);
    返回的outputText;
}

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

这篇关于如何转换日期保存字符串格式的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆