Java SimpleDataFormat转换;下午四时五十分;至;16:50“;

Java SimpleDataFormat转换;下午四时五十分;至;16:50“;,java,simpledateformat,Java,Simpledateformat,我有一个关于从12小时am/pm格式转换为24小时格式的问题。我尝试过使用SimpleDataFormat,但遇到了一些问题 如你所见,我打印了5行原始时间和转换时间,但以“PM”结尾的案例失败。请注意,一些输入为24小时格式,其他输入为12小时am/pm格式 下面是我为转换编写的代码: static String standardizeTime(String inputTime) throws ParseException { SimpleDateFormat[] testI

我有一个关于从12小时am/pm格式转换为24小时格式的问题。我尝试过使用SimpleDataFormat,但遇到了一些问题

如你所见,我打印了5行原始时间和转换时间,但以“PM”结尾的案例失败。请注意,一些输入为24小时格式,其他输入为12小时am/pm格式

下面是我为转换编写的代码:

static String standardizeTime(String inputTime) throws ParseException {
        SimpleDateFormat[] testInputFormat = new SimpleDateFormat[2];

        SimpleDateFormat returnFormat = new SimpleDateFormat("HH:mm");
        testInputFormat[0] = new SimpleDateFormat("hh:mm aa");
        testInputFormat[1] = new SimpleDateFormat("HH:mm");
        
        testInputFormat[0].setLenient(false);
        testInputFormat[1].setLenient(false);

        Date time;
        for (int i = 0; i < testInputFormat.length; i++) {
            try {
                time = testInputFormat[i].parse(inputTime);
                return returnFormat.format(time);
            } catch (ParseException e) {
                continue;
            }
        }

        return "";
    }
静态字符串标准化时间(字符串输入时间)引发异常{
SimpleDataFormat[]testInputFormat=新SimpleDataFormat[2];
SimpleDataFormat returnFormat=新的SimpleDataFormat(“HH:mm”);
testInputFormat[0]=新的SimpleDataFormat(“hh:mm aa”);
testInputFormat[1]=新的SimpleDataFormat(“HH:mm”);
testInputFormat[0]。setLenient(false);
testInputFormat[1]。setLenient(false);
日期时间;
对于(int i=0;i

我应该更改什么来解决此问题?

此操作正常

import java.text.SimpleDateFormat;

public class HelloWorld{

     public static void main(String []args) throws Exception {
         final SimpleDateFormat parser = new SimpleDateFormat("hh:mm aa");
         final SimpleDateFormat printer = new SimpleDateFormat("HH:mm");
        System.out.println(printer.format(parser.parse("4:07 pm")));
     }
}

您的代码看起来不错,所以我认为问题出在别处。

我通过参考以下帖子解决了我的问题:

问题是,我电脑的默认语言不是英语,所以以Alexander的回答为例。我需要写:

import java.text.SimpleDateFormat;
import java.util.Locale;

public class HelloWorld{

     public static void main(String []args) throws Exception {
         final SimpleDateFormat parser = new SimpleDateFormat("hh:mm aa", Locale.ENGLISH);
         final SimpleDateFormat printer = new SimpleDateFormat("HH:mm");
         System.out.println(printer.format(parser.parse("4:07 pm")));
     }
}

注意解析器中的“Locale.ENGLISH”

确保在日期/时间包含字母时,使用带有日期/时间格式API的
Locale
,因为不同的Locale具有不同的字母表示形式

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

public class Main {
    public static void main(String[] args) throws ParseException {
        DateFormat stfInput = new SimpleDateFormat("h:m a", Locale.ENGLISH);// 12-hour format
        DateFormat stfOutput = new SimpleDateFormat("HH:mm", Locale.ENGLISH);// 24-hour format
        Date time = stfInput.parse("02:57 AM");
        System.out.println(stfOutput.format(time));
        time = stfInput.parse("07:05 PM");
        System.out.println(stfOutput.format(time));
    }
}
输出:

02:57
19:05
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("h:m a", Locale.ENGLISH);// 12-hour format
        DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("HH:mm", Locale.ENGLISH);// 24-hour format
        LocalTime time = LocalTime.parse("02:57 AM", dtfInput);
        System.out.println(time.format(dtfOutput));
        time = LocalTime.parse("07:05 PM", dtfInput);
        System.out.println(time.format(dtfOutput));
    }
}
02:57
19:05
注意:
java.util
日期时间类过时且容易出错,它们的格式化API
SimpleDataFormat
也是如此。我建议你完全停止使用它们,转而使用。有关现代日期时间API的更多信息,请访问

如果您是为您的Android项目执行此操作,并且您的Android API级别仍然不符合Java-8,请检查并重试

使用现代日期时间API:

02:57
19:05
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("h:m a", Locale.ENGLISH);// 12-hour format
        DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("HH:mm", Locale.ENGLISH);// 24-hour format
        LocalTime time = LocalTime.parse("02:57 AM", dtfInput);
        System.out.println(time.format(dtfOutput));
        time = LocalTime.parse("07:05 PM", dtfInput);
        System.out.println(time.format(dtfOutput));
    }
}
02:57
19:05
输出:

02:57
19:05
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("h:m a", Locale.ENGLISH);// 12-hour format
        DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("HH:mm", Locale.ENGLISH);// 24-hour format
        LocalTime time = LocalTime.parse("02:57 AM", dtfInput);
        System.out.println(time.format(dtfOutput));
        time = LocalTime.parse("07:05 PM", dtfInput);
        System.out.println(time.format(dtfOutput));
    }
}
02:57
19:05
我应该更改什么来解决此问题

首先也是最重要的一点:我建议您在时间工作中使用java.time,这是一种现代的java日期和时间API。您尝试使用的日期时间类,
SimpleDateFormat
date
,设计糟糕,而且早已过时。现代的API非常好用。阿文德·库马尔·阿维纳什(Arvind Kumar Avinash)的答案的后半部分向您展示了如何。我没有理由重复。是的,Arvind的代码确实解决了你的问题

你的代码出了什么问题? 据说您的问题是没有为格式化程序提供讲英语的区域设置。这是事实的一部分,但它不能完全解释为什么
07:05 PM
被转换为
07:05
,而正确的转换会给出
19:05

发生的情况是,您的第一个输入格式化程序,即具有模式
hh:mm aa
的输入格式化程序,在解析时失败,因为它使用了一种可以称为
PM
的语言。接下来,第二个格式化程序使用pattern
HH:mm
成功地解析了字符串。令人惊讶,不是吗?我认为这是<代码> SimpleDateFormat < /代码>中的许多混淆特性之一,它不一定解析整个字符串。它将
07:05
部分解析为小时和分钟。然后它忽略了字符串的其余部分。这样,您得到的结果是不正确的
07:05
,而不是正确的
19:05
。这只是根本不应该使用
SimpleDateFormat
的原因之一

链接
  • 解释如何使用java.time
  • 相关问题:
    • 关于用于解析的区域设置我是PM。显然,我建议使用java.time
    • 关于
      simpleDataFormat
      不分析整个字符串。通过使用java.time以简单明了的方式解决问题,获得灵感
    • ,我觉得和你的很像
    • 。我建议使用java.time

谢谢你的回答,亚历克斯。正如你所说,问题不在这里。事实证明,我需要在解析器中包含“Locale.ENGLISH”,因为我的计算机的默认语言不是英语。我建议您不要使用
SimpleDateFormat
Date
。这些类设计得很糟糕,而且早已过时,其中前者尤其令人讨厌。而是使用
LocalTime
DateTimeFormatter
,两者都来自。