小编Pav*_*aev的帖子

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

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

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

java.time.format.DateTimeParseException: Text '10:11:12' could not be parsed: Unable …
Run Code Online (Sandbox Code Playgroud)

java java-8 java-time

1
推荐指数
1
解决办法
626
查看次数

标签 统计

java ×1

java-8 ×1

java-time ×1