JodaOrg / joda-time

Joda-Time is the widely used replacement for the Java date and time classes prior to Java SE 8.
http://www.joda.org/joda-time/
Apache License 2.0
4.98k stars 986 forks source link

Format `Hmmss` parses wrong time or throws exception #797

Open randallwhitman opened 1 week ago

randallwhitman commented 1 week ago

Key information

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
  }