Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format Hmmss parses wrong time or throws exception #797

Open
randallwhitman opened this issue Nov 22, 2024 · 0 comments
Open

Format Hmmss parses wrong time or throws exception #797

randallwhitman opened this issue Nov 22, 2024 · 0 comments

Comments

@randallwhitman
Copy link

Key information

  • Joda-Time version - cloned from GitHub this month
  • Result of TimeZone.getDefault() - sun.util.calendar.ZoneInfo[id="Europe/London",offset=0,dstSavings=3600000,useDaylight=true,transitions=242,lastRule=java.util.SimpleTimeZone[id=Europe/London,offset=0,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]]
  • Result of DateTimeZone.getDefault() - Europe/London

Problem description

Maybe the answer is migrate to java.time, but for what it's worth, with time-only format Hmmss, joda-time parses the wrong time or throws exception.

Test case

joda-time
    public void testFormatParse_Hmmss() {
        DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("Hmmss").withZoneUTC();
        DateTime parsed = dateFormatter.parseDateTime("03030");
        assertEquals(0, parsed.getHourOfDay());  // AssertionFailedError: expected:<0> but was:<3>
        assertEquals(30, parsed.getMinuteOfHour());
	parsed = dateFormatter.parseDateTime("71532");  // IllegalFieldValueException: Cannot parse "71532": Value 71 for hourOfDay must be in the range [0,23]
        assertEquals(7, parsed.getHourOfDay());
        assertEquals(15, parsed.getMinuteOfHour());
    }
java.time
  @Test
  public void TestHmmss() throws Exception {
    DateTimeFormatter formatter =
                      DateTimeFormatter.ofPattern("Hmmss");
    LocalTime
        tm = LocalTime.parse("03030", formatter);
    System.out.printf("%s%n", tm);  // 00:30:30
        tm = LocalTime.parse("71532", formatter);
    System.out.printf("%s%n", tm);  // 07:15:32
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant