带自定义模式的JDK8 java.time.LocalTime

时间:2015-03-02 07:31:05

标签: java java-8 java-time

任何人都可以向我解释为什么这个测试通过了:

import org.junit.Assert;
import org.junit.Test;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

import static org.hamcrest.core.Is.is;

public class BasicTest extends Assert{

    @Test
    public void testLocalTimeWithPredefinedPattern() throws Exception {
        DateTimeFormatter dtf = DateTimeFormatter.ISO_TIME;
        LocalTime time = LocalTime.parse("10:11:12", dtf);
        assertThat(time.toString(), is("10:11:12"));
    }

    /**
     * It's a kind of bug from my side of view
     */
    @Test(expected = DateTimeParseException.class)
    public void testLocalTimeWithCustomPattern() throws Exception {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("hh:mm:ss");
        LocalTime time = LocalTime.parse("10:11:12", dtf);
    }
}

第二次测试捕获的异常如下所示:

java.time.format.DateTimeParseException: Text '10:11:12' could not be parsed: Unable to obtain LocalTime from TemporalAccessor: {MilliOfSecond=0, MinuteOfHour=11, MicroOfSecond=0, SecondOfMinute=12, NanoOfSecond=0, HourOfAmPm=10},ISO of type java.time.format.Parsed

这有点不合逻辑,不是吗?

2 个答案:

答案 0 :(得分:8)

' HH'是1-12小时,需要额外的上午/下午标志。将其更改为' HH'它应该按预期工作。

另请注意,在第一次测试中,您可能需要ISO_LOCAL_TIME

答案 1 :(得分:3)

摘要:使用ISO_LOCAL_TIME,而不是ISO_TIME并使用" H"而不是" h"。

在调查java.time的解析问题时,请务必检查错误消息。在这种情况下,消息是:

Text '10:11:12' could not be parsed:
Unable to obtain LocalTime from TemporalAccessor:
{MilliOfSecond=0, MinuteOfHour=11, MicroOfSecond=0,
SecondOfMinute=12, NanoOfSecond=0, HourOfAmPm=10},ISO
of type java.time.format.Parsed

(格式化为在StackOverflow中易于阅读)

该消息告诉我们填充的字段是:

  • HourOfAmPm = 10
  • MinuteOfHour = 11
  • SecondOfMinute = 12

它是" HourOfAmPm"而不是" HourOfDay"告诉我们使用了错误的模式字母," h"而不是" H"。关键是在java.time中,填充字段集的解析方式比旧格式化程序DateFormat更严格。获得LocalTime," HourOfDay"或" AmPmOfDay"和" HourOfAmPm",必须指定。