【发布时间】:2017-12-29 05:30:51
【问题描述】:
当我将参数传递为“09:30 AM”时,它会在 android 中返回异常 显示无法解析的日期:“09:30 AM”(偏移量 6)
public static String convertTo24Hour (String now){
String time24="";
try {
SimpleDateFormat inFormat = new SimpleDateFormat("hh:mm a");
SimpleDateFormat outFormat = new SimpleDateFormat("HH:mm");
time24 = outFormat.format(inFormat.parse(now));
} catch (Exception e) {
System.out.println("Exception : " + e.getMessage());
}
return time24;
}
【问题讨论】:
-
使用正确的语言环境。
-
你确定你没有遗漏任何东西吗?因为代码看起来不错。尝试分配 Locale 。
-
代码正确,显示完整的异常日志。
-
顺便说一句,即使在 Android 上,也可以考虑扔掉长期过时且臭名昭著的麻烦
SimpleDateFormat和朋友,并将 ThreeTenABP 添加到您的项目中,以便使用现代 Java 日期java.time和时间 API。使用起来感觉好多了。