【问题标题】:Getting UnParsable Date Error for hh:mm a format获取 hh:mm 格式的不可解析日期错误
【发布时间】:2017-10-25 08:08:25
【问题描述】:

SimpleDateFormat hh:mm a 的时间是下午 1:32 我在棉花糖设备三星上遇到错误 但适用于三星奇巧标签版 为什么会这样..

代码

Date arrStart = new SimpleDateFormat("hh:mm a").parse(startTime);

确切的错误:

Unparseable date: "01:32 PM" (at offset 6)

【问题讨论】:

标签: android datetime android-6.0-marshmallow simpledateformat android-4.4-kitkat


【解决方案1】:

AM/PM 可被视为当地时间的错误。试试下面的格式:

   String s = "01:32 PM";
   Date time = null;
   DateFormat parseFormat = new SimpleDateFormat("hh:mm a", Locale.ENGLISH);
   System.out.println(time = parseFormat.parse(s));

你也可以使用

LocalTime time = null;
DateTimeFormatter parseFormatter 
    = DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH);
try {
    time = LocalTime.parse(s, parseFormatter);
} catch (DateTimeParseException dtpe) {
    System.out.println(dtpe.getMessage());
}

【讨论】:

  • 为我工作。
猜你喜欢
  • 2017-09-10
  • 2018-06-18
  • 2020-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多