【发布时间】:2018-05-31 05:59:53
【问题描述】:
在此代码中,如果是 12:01:00,则显示错误的时间,它显示的是上午 12:00,但正确的时间是下午 12:00:
private static final String sourceFormat = "hh:mm:ss";
private static final String targetFormat = "hh:mm a";
public static String convertTimeFormat(String dateStr) {
if (dateStr.equals("")) {
return "";
}
Log.d("date", dateStr + "---" + sourceFormat + "---" + targetFormat);
SimpleDateFormat form = new SimpleDateFormat(sourceFormat);
Date date = null;
try {
date = form.parse(dateStr);
} catch (Exception e) {
e.printStackTrace();
}
SimpleDateFormat postFormater = new SimpleDateFormat(targetFormat);
String newDateStr = postFormater.format(date);
Log.d("Lead Response", newDateStr);
return newDateStr;
}
非常感谢您在此问题上的时间和帮助。
【问题讨论】:
-
如果您的源时间是 24 小时格式,您需要在 sourceFormat 中使用 'HH' 而不是 'hh'。
-
@HareshChhelana 它不工作
-
@AkshayBarodia 如果它不起作用那么为什么你接受 Venki WAR 的答案。