Java 将 XX:XX AM/PM 转换为 24 小时制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23316089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:49:54  来源:igfitidea点击:

Converting XX:XX AM/PM to 24 Hour Clock

javastringtime

提问by user3505931

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.

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

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?

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

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

采纳答案by Braj

Try

尝试

  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)));

output:

输出:

15:30

回答by Matej ?pilár

SimpleDateFormat inFormat = new SimpleDateFormat("hh:mm aa");
SimpleDateFormat outFormat = new SimpleDateFormat("HH:mm");
String time24 = outFormat.format(inFormat.parse(yourTimeString));

also you can read more about converting times here http://deepeshdarshan.wordpress.com/2012/08/17/how-to-change-time-from-12-hour-format-to-24-hour-format-in-java/

您也可以在此处阅读有关转换时间的更多信息http://deepeshdarshan.wordpress.com/2012/08/17/how-to-change-time-from-12-hour-format-to-24-hour-format-in -java/

回答by Rod_Algonquin

try this: 

String string = "3:35 PM";
    Calendar calender = Calendar.getInstance();
    DateFormat format = new SimpleDateFormat( "hh:mm aa");
    Date date;
    date = format.parse( string );
    calender.setTime(date);

    System.out.println("Hour: " + calender.get(Calendar.HOUR_OF_DAY));
    System.out.println("Minutes: " + calender.get(Calendar.MINUTE))

;

;

works fine and same result as what you would like.

工作正常,结果与您想要的相同。

回答by slaadvak

Here is a link to the SimpleDateFormat Javadoc

这是SimpleDateFormat Javadoc的链接

This is the way to go :

这是要走的路 :

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class TimeParsing {

    public static void main(String[] args) {
        try {
            // Declare a date format for parsing
            SimpleDateFormat dateParser = new SimpleDateFormat("h:mm a");

            // Parse the time string
            Date date = dateParser.parse("3:30 PM");

            // Declare a date format for printing
            SimpleDateFormat dateFormater = new SimpleDateFormat("HH:mm");

            // Print the previously parsed time
            System.out.println(dateFormater.format(date));

        } catch (ParseException e) {
            System.err.println("Cannot parse this time string !");
        }
    }
}

Console output is : 15:30

控制台输出是: 15:30

回答by Manny Shirazy

Mine did not work till I added Locale like this:

在我像这样添加 Locale 之前,我的没有工作:

SimpleDateFormat date12Format = new SimpleDateFormat("hh:mm aa", Locale.US);