在java中将时间从12小时转换为24小时

kot*_*oti 23 java time

在我的应用程序中,我要求将12小时的时间格式化为24小时.我必须使用哪种方法?

例如,时间如上午10:30.我怎样才能在java中转换为24小时的时间?

Bar*_*ers 70

试试这个:

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

public class Main {
   public static void main(String [] args) throws Exception {
       SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm");
       SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a");
       Date date = parseFormat.parse("10:30 PM");
       System.out.println(parseFormat.format(date) + " = " + displayFormat.format(date));
   }
}
Run Code Online (Sandbox Code Playgroud)

产生:

10:30 PM = 22:30
Run Code Online (Sandbox Code Playgroud)

请参阅:http://download.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html

  • 仅供参考,非常麻烦的旧日期时间类,例如 [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html)、` java.util.Calendar` 和 `java.text.SimpleDateFormat` 现在是[遗留](https://en.wikipedia.org/wiki/Legacy_system),被[*java.time*](https:/ /docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) Java 8 及更高版本中内置的类。请参阅 Oracle 的 [*教程*](https://docs.oracle.com/javase/tutorial/datetime/TOC.html)。请参阅 [云解答](/sf/answers/2617196711/) 中使用 *java.time* 的现代解决方案。 (3认同)

Clo*_*oud 16

java.time

在Java 8及更高版本中,可以使用类java.time.LocalTime在一行中完成它.

在格式化模式中,小写hh表示12小时制,而大写HH表示24小时制.

代码示例:

String result =                                       // Text representing the value of our date-time object.
    LocalTime.parse(                                  // Class representing a time-of-day value without a date and without a time zone.
        "03:30 PM" ,                                  // Your `String` input text.
        DateTimeFormatter.ofPattern(                  // Define a formatting pattern to match your input text.
            "hh:mm a" ,
            Locale.US                                 // `Locale` determines the human language and cultural norms used in localization. Needed here to translate the `AM` & `PM` value.
        )                                             // Returns a `DateTimeFormatter` object.
    )                                                 // Return a `LocalTime` object.
    .format( DateTimeFormatter.ofPattern("HH:mm") )   // Generate text in a specific format. Returns a `String` object.
;
Run Code Online (Sandbox Code Playgroud)

请参阅IdeOne.com上的代码.

15:30

请参阅Oracle教程.


fvu*_*fvu 8

假设您隐式或显式地使用SimpleDateFormat,您需要使用H而不是h格式字符串.

例如

HH:mm:ss

代替

hh:mm:ss


Arp*_*ini 5

12 到 24 小时时间转换,如果更改输出和输入 SimpleDateFormat 类参数中的时间格式,则可以反转

测试数据输入:

字符串输入=“07:05:45PM”;timeCoversion12to24(输入);

输出

19:05:45

 public static String timeCoversion12to24(String twelveHoursTime) throws ParseException {

        //Date/time pattern of input date (12 Hours format - hh used for 12 hours)
        DateFormat df = new SimpleDateFormat("hh:mm:ssaa");

        //Date/time pattern of desired output date (24 Hours format HH - Used for 24 hours)
        DateFormat outputformat = new SimpleDateFormat("HH:mm:ss");
        Date date = null;
        String output = null;

        //Returns Date object
        date = df.parse(twelveHoursTime);

        //old date format to new date format
        output = outputformat.format(date);
        System.out.println(output);

        return output;
    }
Run Code Online (Sandbox Code Playgroud)