将 XX:XX AM/PM 转换为 24 小时制 [英] Converting XX:XX AM/PM to 24 Hour Clock

查看:40
本文介绍了将 XX:XX AM/PM 转换为 24 小时制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在谷歌上搜索过,但我不知道如何获取字符串:xx:xx AM/PM(例如下午 3:30)并将其更改为现在是 24小时.

I have searched google and I cannot find out how you can take a string: xx:xx AM/PM (ex. 3:30 PM) and change it so that it is now in 24 hours.

例如,之前的时间是15:30".我已经研究过简单地使用 if then 语句来操作字符串,但这似乎很乏味.有什么简单的方法可以做到这一点吗?

So for example the previous time would be "15:30". I have looked into simply using if then statements to manipulate the string, however it seems very tedious. Is there any easy way to do this?

Input: 3:30 PM
Expected Output:  15:30

推荐答案

尝试

  String time = "3:30 PM";

    SimpleDateFormat date12Format = new SimpleDateFormat("hh:mm a");

    SimpleDateFormat date24Format = new SimpleDateFormat("HH:mm");

    System.out.println(date24Format.format(date12Format.parse(time)));

输出:

15:30

这篇关于将 XX:XX AM/PM 转换为 24 小时制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆