ZonedDateTimeオブジェクトに日付と時刻の文字列を解析

ME:

私は知られているタイムゾーンの日付と時刻の文字列を解析しようとしています。文字列の形式は次のとおりです。

2019-03-07 00:05:00-05:00

私はこれを試してみました:

package com.example.test;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class Test {

    public static void main( String[] args ) {

        ZoneId myTimeZone = ZoneId.of("US/Eastern");

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ssXX");

        ZonedDateTime zdt = ZonedDateTime.parse("2019-03-07 00:05:00-05:00", dateTimeFormatter.withZone(myTimeZone));

        System.out.println(zdt);

    }

}

これがスローされた例外です。

Exception in thread "main" java.time.format.DateTimeParseException: Text '2019-03-07 00:05:00-05:00' could not be parsed at index 19
    at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
    at java.time.ZonedDateTime.parse(ZonedDateTime.java:597)
    at com.example.test.Test.main(Test.java:24)
C:\Users\user\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

私は、Java 1.8.0_191を使用しています。

amseager:

このパターンを使用します。 yyyy-MM-dd HH:mm:ssXXX

以下からのドキュメント

オフセットXとX:... 2つの文字は、そのような「0130」として、コロンなしで、時間と分を出力します。「:30 01」の3文字のような、コロンで、時間と分を出力します。

あなたの文字列がタイムゾーン内のコロンが含まれているのであれば、あなたは3、「X-ES」を使用する必要があります。

そして、資本Yは「週ベース年」ではなく、通常の1(y)を意味します。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=223055&siteId=1