【问题标题】:How to convert date saved in String format to Date format [duplicate]如何将以字符串格式保存的日期转换为日期格式[重复]
【发布时间】:2010-06-29 13:45:42
【问题描述】:

可能重复:
DateFormat conversion problem in java?

我正在尝试将字符串转换为日期,但出现错误。

我正在使用:

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

然后我将此日期保存在一个文件中,该文件以以下格式保存:

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

然后当我从文件中读取这个日期作为字符串时,我再次想将它保存到一个日期,但我得到了错误。

我试过了:

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

但我收到错误:

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

有什么办法可以找回日期。

谢谢

【问题讨论】:

  • 我已经遇到了这个问题。检查发给我的reto meier's answer。希望对您有所帮助。

标签: android date


【解决方案1】:
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;
}

【讨论】:

    【解决方案2】:

    试试这个。必须指定正确的日期格式。

    SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
    Date d = format.parse(fileDate);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-16
      • 2013-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多