在 UTC 范围内将 JAVA 中的 12 小时制转换为 24 小时制

Convert 12 Hour to 24 Hour in JAVA within scope of UTC

请帮我将时间从 12 小时格式转换为 24 小时格式 return 时间应该是 JAVA

中的 UTC

这将帮助您将时间转换为 24 小时制

public static String convertTo24Hour(String Time) {
    DateFormat f1 = new SimpleDateFormat("hh:mm a"); //11:00 pm
    Date d = null;
    try {
        d = f1.parse(Time);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    DateFormat f2 = new SimpleDateFormat("HH:mm");
    String x = f2.format(d); // "23:00"

    return x;
}

稍后您可以根据自己的意愿转换为 UTC,或者您可以在一个函数中编译代码

public static String CalculateUTC_Time(String Date, String Time) {
    String X[] = Time.split(":");

    int Hours = Integer.parseInt(X[0]);
    int Minutes = Integer.parseInt(X[1]);

    LocalDate date = new LocalDate(Date);
    LocalTime time = new LocalTime(Hours, Minutes);
    DateTime dt = date.toDateTime(time);

    SimpleDateFormat f2 = new SimpleDateFormat("HH:mm");
    f2.setTimeZone(TimeZone.getTimeZone("UTC"));
    return (f2.format(dt.toDate()));
}