Java 分析一位数小时时间时出错:凌晨3:00

Java 分析一位数小时时间时出错:凌晨3:00,java,time,datetime-parsing,java-time,Java,Time,Datetime Parsing,Java Time,所以我一直在尝试将字符串Hour从12小时格式转换为24小时格式,代码如下: public String convertAM(String checkinCheckoutTime) { String withoutAM = checkinCheckoutTime.replaceAll("AM", "").trim(); if (withoutAM.length() == 1 || withoutAM.length() == 2) { return LocalTime.parse(

所以我一直在尝试将字符串Hour从12小时格式转换为24小时格式,代码如下:

public String convertAM(String checkinCheckoutTime) {
String withoutAM = checkinCheckoutTime.replaceAll("AM", "").trim();
if (withoutAM.length() == 1 || withoutAM.length() == 2) {
  return LocalTime.parse(
      withoutAM + ":00 AM", DateTimeFormatter.ofPattern("hh:mm a", Locale.US))
      .format(DateTimeFormatter.ofPattern("HH:mm"));
} else {
  return LocalTime.parse(
      checkinCheckoutTime, DateTimeFormatter.ofPattern("hh:mm a", Locale.US))
      .format(DateTimeFormatter.ofPattern("HH:mm"));
}
}

我在这次考试中得了绿色:

    @Test
  public void convertCheckinCheckoutTime12AMWithSpace() {
    String checkOutTime = "12 AM";
    String expectedCheckOutTime = "00:00";

    String result = this.service.convertAM(checkOutTime);

    Assert.assertEquals(expectedCheckOutTime, result);
  }
但在这个测试中,我得到了一个错误:

    @Test
  public void convertCheckinCheckoutTimeAMNoRangeWithSpace() {
    String checkOutTime = "3 AM";
    String expectedCheckOutTime = "3:00";

    String result = this.service.convertAM(checkOutTime);

    Assert.assertEquals(expectedCheckOutTime, result);
  }
错误是:

java.time.format.DateTimeParseException: Text '3:00 AM' could not be parsed at index 0

我可以知道凌晨3点出了什么事吗?提前谢谢

结果我需要更改一位数或两位数小时的格式,如果12的格式是hh:mm,如果3的格式是h:mm

结果我需要更改一位数或两位数小时的格式,如果12的格式是hh:mm,如果3的格式是h:mm

3 AM应该是
03 AM
。您可以按如下方式更新代码:

。。。
如果(没有AM.length()==1 | |没有AM.length()==2){
如果(不带am.length()==1){
withoutAM=“0”+withoutAM;
}
...

convertAM
功能中

3 AM应该是
03 AM
。您可以如下方式更新代码:

。。。
如果(没有AM.length()==1 | |没有AM.length()==2){
如果(不带am.length()==1){
withoutAM=“0”+withoutAM;
}
...

convertAM
function

中,不要发送凌晨3点,尝试发送凌晨3点,这样可以正常工作。但是在使用PM时,您应该使用此方法替换您的方法

public static String convertAM(String checkinCheckoutTime) {
            String withoutAM = checkinCheckoutTime.replaceAll("AM", "").trim();
            String withoutPM = checkinCheckoutTime.replaceAll("PM", "").trim();
            System.out.println(withoutAM);
            System.out.println(withoutPM);
            if (withoutAM.length() == 1 || withoutAM.length() == 2) {
                System.out.println("length below 2");
                return LocalTime.parse(withoutAM + ":00 AM", DateTimeFormatter.ofPattern("hh:mm a", Locale.US))
                        .format(DateTimeFormatter.ofPattern("HH:mm"));
            } else {
                System.out.println("lengh high");
                return LocalTime.parse(withoutPM + ":00 PM", DateTimeFormatter.ofPattern("hh:mm a", Locale.US))
                        .format(DateTimeFormatter.ofPattern("HH:mm"));
            }
        }

不要在凌晨3点发送邮件,尝试在凌晨3点发送邮件,这样可以很好地工作。但是在使用PM时,您应该用此方法替换您的方法

public static String convertAM(String checkinCheckoutTime) {
            String withoutAM = checkinCheckoutTime.replaceAll("AM", "").trim();
            String withoutPM = checkinCheckoutTime.replaceAll("PM", "").trim();
            System.out.println(withoutAM);
            System.out.println(withoutPM);
            if (withoutAM.length() == 1 || withoutAM.length() == 2) {
                System.out.println("length below 2");
                return LocalTime.parse(withoutAM + ":00 AM", DateTimeFormatter.ofPattern("hh:mm a", Locale.US))
                        .format(DateTimeFormatter.ofPattern("HH:mm"));
            } else {
                System.out.println("lengh high");
                return LocalTime.parse(withoutPM + ":00 PM", DateTimeFormatter.ofPattern("hh:mm a", Locale.US))
                        .format(DateTimeFormatter.ofPattern("HH:mm"));
            }
        }
解释 您使用的是小时模式
hh
。这需要两位数的小时,例如
12
03
。但您的输入是一位数,
3
用于小时字段


解决方案 要么将输入调整为两位数,以使
03
而不是
3
。要么使用一个数字就可以的模式,即
h

从:

h、 上午1点至下午12点,12号

数字:如果字母的计数为1,则使用最小位数输出值,且不带填充。否则,数字的计数用作输出字段的宽度,并根据需要填充值零。以下图案字母对计数有限制字母。只能指定一个字母“c”和“F”。最多可以指定两个字母“d”、“H”、“H”、“K”、“K”、“m”和“s”。最多可以指定三个字母“d”


笔记 通过移出唯一不同的部分,即输入字符串,您可以简化代码并减少重复。由于您返回了
if
,因此
else
是不必要的。您还可以通过拆分一些嵌套语句并将其放入变量中,进一步简化代码并使代码更具可读性。Yo你还应该添加一条快速评论,解释发生了什么:

DateTimeFormatter inputFormatter =  DateTimeFormatter.ofPattern("hh:mm a", Locale.US);
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("HH:mm", Locale.US);

// Patch time without minutes, 3 AM to 3:00 AM
String withoutAM = checkinCheckoutTime.replaceAll("AM", "").trim();
boolean hasOnlyHours = withoutAM.length() == 1 || withoutAM.length() == 2;
String timeInput = hasOnlyHours ? withoutAM + ":00 AM" : checkinCheckoutTime;

return LocalTime.parse(timeInput, inputFormatter)
      .format(outputFormatter);
请注意,您的代码对于诸如
3pm
之类的输入不起作用,因为您仅删除
AM
。您可以简单地添加另一个调用来同时删除
PM
,或者将这两个调用放在一个正则表达式中,因为您使用的是regex replace
replaceAll
而不是非regex
replace

String withoutSuffix = checkinCheckoutTime.replaceAll("(AM|PM)", "").trim();
长度检查可以简化为
解释
您使用的是小时模式
hh
。这需要两位数的小时,例如
12
03
。但您的输入是一位数,
3
用于小时字段


解决方案 要么将输入调整为两位数,以使
03
而不是
3
。要么使用一个数字就可以的模式,即
h

从:

h、 上午1点至下午12点,12号

数字:如果字母的计数为1,则使用最小位数输出值,且不带填充。否则,数字的计数用作输出字段的宽度,并根据需要填充值零。以下图案字母对计数有限制字母。只能指定一个字母“c”和“F”。最多可以指定两个字母“d”、“H”、“H”、“K”、“K”、“m”和“s”。最多可以指定三个字母“d”


笔记 通过移出唯一不同的部分,即输入字符串,您可以简化代码并减少重复。由于您返回了
if
,因此
else
是不必要的。您还可以通过拆分一些嵌套语句并将其放入变量中,进一步简化代码并使代码更具可读性。Yo你还应该添加一条快速评论,解释发生了什么:

DateTimeFormatter inputFormatter =  DateTimeFormatter.ofPattern("hh:mm a", Locale.US);
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("HH:mm", Locale.US);

// Patch time without minutes, 3 AM to 3:00 AM
String withoutAM = checkinCheckoutTime.replaceAll("AM", "").trim();
boolean hasOnlyHours = withoutAM.length() == 1 || withoutAM.length() == 2;
String timeInput = hasOnlyHours ? withoutAM + ":00 AM" : checkinCheckoutTime;

return LocalTime.parse(timeInput, inputFormatter)
      .format(outputFormatter);
请注意,您的代码对于诸如
3pm
之类的输入不起作用,因为您仅删除
AM
。您可以简单地添加另一个调用来同时删除
PM
,或者将这两个调用放在一个正则表达式中,因为您使用的是regex replace
replaceAll
而不是非regex
replace

String withoutSuffix = checkinCheckoutTime.replaceAll("(AM|PM)", "").trim();

长度检查可以简化为仅使用
TL;DR仅使用格式模式字符串
h[:mm]a

我希望您能告诉我们您最初的时间字符串是什么样子。对于这个答案,我假设:

  • 上午或下午的小时数可以是1或2位数
  • 以冒号分隔的两位数分钟可能存在或不存在
  • 由空格分隔的AM或PM始终存在
所有这些都可以通过一个单一格式的模式字符串来处理,因此在解析字符串之前无需修改字符串,我发现不修改字符串更简单。诀窍包括:

  • 一个模式字母
    h
    匹配一个或两个数字小时,例如
    3
    03
    12
  • 方括号包含格式的可选部分。因此
    [:mm]
    匹配可选的冒号和分钟
要看到它的作用:

    DateTimeFormatter timeFormatter
            = DateTimeFormatter.ofPattern("h[:mm] a", Locale.US);

    String[] timeStrings = {
            "3 AM", "12 AM", "3:00 AM", "3:40 AM", "12 PM", "12:20 PM", "4 PM", "04 PM"
    };
    for (String ts : timeStrings) {
        LocalTime time = LocalTime.parse(ts, timeFormatter);
        System.out.format(Locale.ENGLISH, "%-8s is parsed into %s%n", ts, time);
    }
输出为:

这说明我对你的整个a不满意