如何转换24小时格式的时间为12小时格式?小时、格式、时间为

2023-09-08 08:31:46 作者:哥、不愿将就

在我的应用程序,我想转换给定的CDT格式化为CDT 24小时字符串格式化的12小时字符串,如何转换给定的24小时格式字符串中的12小时格式字符串??

In my application i want to convert the given CDT formatted 24 hr string in to CDT formatted 12 hr string, How to convert a given 24 hr format string in to 12 hr format string??

推荐答案

您可以尝试使用的SimpleDateFormat 对象转换的时间格式。

you can try using a SimpleDateFormat object to convert the time formats.

final String time = "23:15";

try {
    final SimpleDateFormat sdf = new SimpleDateFormat("H:mm");
    final Date dateObj = sdf.parse(time);
    System.out.println(dateObj);
    System.out.println(new SimpleDateFormat("K:mm").format(dateObj));
} catch (final ParseException e) {
    e.printStackTrace();
}

这里是SimpleDateFromat的Javadoc 链接。