【问题标题】:Android - SimpleDateFormat: Unparseable dateAndroid - SimpleDateFormat:无法解析的日期
【发布时间】:2017-05-04 10:21:12
【问题描述】:

我在日期问题上卡住了一段时间。 我正在尝试比较两个日期:现在和另一个日期。

我遇到了一个我似乎无法理解的错误,当前日期不可解析。

Unparseable date: "04/05/2017" (at offset 3)

让我与你分享我的代码:

try {
    String date_now = new SimpleDateFormat("dd/MM/yy").format(new Date());
    Date today = new SimpleDateFormat("dd/MMM/yy").parse(date_now);
    Date date_expiry = new SimpleDateFormat("dd/MM/yy").parse(expiry);

    if(date_expiry.before(today)){
        Log.e("TAG", "Expired!");
    }

} catch (ParseException e) {
    Log.e("TAG", e.getMessage());
}

非常感谢您的帮助!

【问题讨论】:

  • 应该是yyyy
  • 你的第二个日期格式可能也不应该有 MMM。
  • 尝试了你的两个建议......它的工作原理:) 真的很抱歉

标签: java android date simpledateformat unparseable


【解决方案1】:

您可以简单地按照 speps 进行操作

String startDate = "06/27/2007";
DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
Date startDate = df.parse(startDate);

【讨论】:

  • 当然可以遵循规范。不能声明两个变量都命名为startDate。格式模式中的小写mm 是分钟。根据规格。 :-P
猜你喜欢
  • 2017-02-28
  • 2013-05-23
  • 1970-01-01
  • 1970-01-01
  • 2011-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多