zoukankan      html  css  js  c++  java
  • JodaTime报时区异常错误

    在将爬下来的网页解析需要的字段批量入口的时候(逻辑类似下面):

    @Test
    public void test_001(){
    
        String TIME = "1990-04-15";
        DateTime dateTime = DateTime.parse(TIME);
        System.out.println(dateTime);
    
    }

    程序抛出了一个时区异常:

    org.joda.time.IllegalInstantException: Cannot parse "1990-04-15": Illegal instant due to time zone offset transition (Asia/Shanghai)
    
    	at org.joda.time.format.DateTimeParserBucket.computeMillis(DateTimeParserBucket.java:471)
    	at org.joda.time.format.DateTimeParserBucket.computeMillis(DateTimeParserBucket.java:411)
    	at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:928)
    	at org.joda.time.DateTime.parse(DateTime.java:160)
    	at org.joda.time.DateTime.parse(DateTime.java:149)
    	at org.cc11001100.Test_001.test_001(Test_001.java:15)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:498)
    	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
    	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
    

    这个是因为我们设置的时区不是东八区,所以解析的时候不应该使用DateTime而应该使用LocalDateTime:

    @Test
    public void test_001(){
    
        String TIME = "1990-04-15";
    //        DateTime dateTime = DateTime.parse(TIME);
    //        System.out.println(dateTime);
    
        LocalDateTime localDateTime = LocalDateTime.parse(TIME);
        System.out.println(localDateTime);
    
    }

    使用LocalDateTime时会识别使用当前所在的时区。

  • 相关阅读:
    英文半字节压缩编码技术
    博弈翻硬币游戏
    POJ 2015 Permutation Code
    8051、ARM和DSP指令周期的测试与分析
    Huffman编码
    CentOS 命令提示符颜色及样式详解
    JAVA程序员面试32问
    面向抽象编程:接口和抽象类
    初学实用:实例讲解Java中的接口的作用
    C#和Java的区别
  • 原文地址:https://www.cnblogs.com/cc11001100/p/7226601.html
Copyright © 2011-2022 走看看