2016-05-17 3 views
6

日時文字列をOffsetDateTimeとして解析できないため、次のエラーが発生するのはなぜですか?なぜ、 `GMT + 8`はdocからまっすぐにコピーされたにもかかわらずパターン` O`で解析できないのですか?

String inputOdt = "2016-01-23T12:34:56 GMT+8"; 
DateTimeFormatter formatterOdt = DateTimeFormatter.ofPattern ("yyyy-MM-dd'T'HH:mm:ss O"); 
OffsetDateTime odt = OffsetDateTime.parse (inputOdt , formatterOdt); 

Java(TM) SE Runtime Environment (build 1.8.0_92-b14)をMac OS Xで使用する場合。El Capitan 10.11.4。

は、エラーが発生します。

Exception in thread "main" java.time.format.DateTimeParseException: Text '2016-01-23T12:34:56 GMT+8' could not be parsed: String index out of range: 25

offset-from-UTCストリングGMT+8DateTimeFormatterのクラス・ドキュメントの例からコピー、貼り付けられています。引用すると:

Offset O: This formats the localized offset based on the number of pattern letters. One letter outputs the short form of the localized offset, which is localized offset text, such as 'GMT', with hour without leading zero, optional 2-digit minute and second if non-zero, and colon, for example 'GMT+8'.


を文字列の残りの部分はLocalDateTimeとして成功裏に解析します。ですから、この問題は実際にはUTCからのオフセットと思われます。

String inputLdt = "2016-01-23T12:34:56"; 
DateTimeFormatter formatterLdt = DateTimeFormatter.ofPattern ("yyyy-MM-dd'T'HH:mm:ss"); 
LocalDateTime ldt = LocalDateTime.parse (inputLdt , formatterLdt); 

System.out.println (""); 
System.out.println ("inputLdt: " + inputLdt); 
System.out.println ("ldt: " + ldt); 

inputLdt: 2016-01-23T12:34:56

ldt: 2016-01-23T12:34:56


部分的な問題を回避するには、入力文字列と書式パターンの両方に末尾にスペースを追加することです。これはうまくいく。

String input = "Sat May 02 2015 00:00:00 GMT+08 "; // Trailing space. 
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ("EEE MMM dd yyyy HH:mm:ss O "); // Trailing space. 
OffsetDateTime odt = OffsetDateTime.parse (input , formatter); // SUCCEEDS 

しかし、コロンなし分を追加すると、単一のOでの作業として文書化され、それが失敗しています。このような場合、末尾のSPACEの回避策は役立ちません。この例ではGMT+0800であるのに対して、上に示したGMT+08では、この例は失敗しますが、上の例は成功します。

String input = "Sat May 02 2015 00:00:00 GMT+0800 "; // Minutes in the offset, and trailing space. 
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ("EEE MMM dd yyyy HH:mm:ss O "); // Trailing space. 
OffsetDateTime odt = OffsetDateTime.parse (input , formatter); // FAILS 
+2

これは 'StringIndexOutOfBoundsException'が、私はこれはバグであると思わせるという事実をあなたはそれを得た –

+2

@SotiriosDelimanolis:https://でバグを。 openjdk.java.net/browse/JDK-8154050 – Andreas

+0

フォーマット文字列と解析するテキストの両方にスペースを追加するのが回避策のようです。 –

答えて

2

Javaのバグと思われます。 https://bugs.openjdk.java.net/browse/JDK-8154050を参照してください:

java.time.format.DateTimeFormatter can't parse localized zone-offset

The DateTimeFormatter fails to parse its own output for format strings containing "O". The following code throws a StringIndexOutOfBoundsException on the final line.

import java.time.ZoneOffset 
import java.time.ZonedDateTime 
import java.time.format.DateTimeFormatter 
DateTimeFormatter formatter = DateTimeFormatter 
     .ofPattern("yyyy-MM-dd'T'HH:mm:ss.S O") 
     .withLocale(Locale.ENGLISH) 
String date = formatter.format(ZonedDateTime.now(ZoneOffset.UTC)); 
formatter.parse(date) 

ERROR MESSAGES/STACK TRACES THAT OCCUR : java.time.format.DateTimeParseException: Text '2016-04-08T10:49:52.7 GMT' could not be parsed: String index out of range: 25

、コメントで:

Attached test case executed on:
JDK 8 - Fail
JDK 8u77 - Fail
JDK 9EA - Fail

それはJavaの9ビルドB116に固定されたようです。

0

私は同じ問題があります。
私の文字列は、 "28.04.2010 09:39:33 UTC + 2"のようなものです。

オフセット(「UTC + 02」)に0を追加する必要があります。それを解析する。

public final static String INPUT_PATTERN_DD_MM_YYYY_HH_mm_ss_zzz = "dd.MM.yyyy HH:mm:ss zzz"; 

オフセットがゼロ(番号なし "UTC" または "GMT")、私はDateTimeFormatterBuilderを使用していますことかもしれませんが:

DateTimeFormatter formatter = new DateTimeFormatterBuilder().parseCaseInsensitive().append(df).optionalStart() 
      .appendPattern("X").optionalEnd().toFormatter(); 

を "X" がある場合は、私が使用していたパターンとして ゾーン・オフセット...

しかし、その後、私はまた、必要があります。

 ZoneId id = ZoneOffset.ofHours(Integer.valueOf(offset)); 

     zonedDateTime = zonedDateTime.withZoneSameInstant(id); 
     zonedDateTime = zonedDateTime.minusHours(Integer.valueOf(offset)); 

本当に厄介な...:-(

私は、java 9は、右の仕事をすることを願っています

関連する問題