twirain
V2EX  ›  问与答

字符串时间转 ZonedDateTime 出现的问题

  •  
  •   twirain · Nov 12, 2018 · 2968 views
    This topic created in 2915 days ago, the information mentioned may be changed or developed.
    String time = "11 12 21:29:01 +0800 2018";
    ZonedDateTime c = ZonedDateTime.parse(time, DateTimeFormatter.ofPattern("MM dd HH:mm:ss xx yyyy"));
    System.out.println(c);
    		
    String time2 = "Nov 12 21:29:01 +0800 2018";
    ZonedDateTime b = ZonedDateTime.parse(time2, DateTimeFormatter.ofPattern("MMM dd HH:mm:ss xx yyyy"));
    System.out.println(b);
    

    第一个输出为:2018-11-12T21:29:01+08:00

    第二个直接抛了异常:Exception in thread "main" java.time.format.DateTimeParseException: Text 'Nov 12 21:29:01 +0800 2018' could not be parsed at index 0

    3 replies  •  2018-11-13 18:42:14 +08:00
    wdlth
    wdlth
    Nov 12, 2018
    1
    https://repl.it/repls/GlamorousSpecializedGnuassembler

    没有发现异常,你是不是输入了全角空格之类的?
    OscarUsingChen
    OscarUsingChen
    Nov 12, 2018
    2
    Locale 的问题?
    twirain
    twirain
    OP
    Nov 13, 2018 via Android
    3
    用 eclipse 编的,提示 unknown source,参数也都勾上了,不知道什么问题
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1513 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 23:12 · PVG 07:12 · LAX 15:12 · JFK 18:12
    ♥ Do have faith in what you're doing.