【发布时间】:2021-11-14 17:50:15
【问题描述】:
public static String getTime(String time) {
String ampmTime = null;
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss", Locale.getDefault());
Date dt;
try {
dt = sdf.parse(time);
SimpleDateFormat sdfs = new SimpleDateFormat("hh:mm a", Locale.getDefault());
if (dt != null) {
ampmTime = sdfs.format(dt);
}
} catch (ParseException e) {
e.printStackTrace();
}
return ampmTime;
}
在参数字符串中,我发送的是 12:00:00 格式,所以我希望它是 12:00pm 而不是 12:00am。其他时间正确显示,例如 13:30:00 到 1:30pm 。所以请帮帮我??
【问题讨论】:
-
请考虑扔掉早已过时且臭名昭著的麻烦
SimpleDateFormat和朋友。看看您是否可以使用desugaring 或将ThreeTenABP 添加到您的Android 项目中,以便使用现代Java 日期和时间API 的java.time。使用起来感觉好多了。 -
LocalTime.parse("12:00:00").format(DateTimeFormatter.ofPattern("hh:mm a", Locale.forLanguageTag("en-IN")))。根据要求产生12:00 PM。是的,确实如此,使用 java.time 您只需要一个格式化程序。请自己分解成适当数量的语句。