2017-06-11 34 views
-2

私はちょうどはZonedDateTimeに文字列を変換できません:DateTimeParseException

static String getWatchTime(JSONObject aJson, JSONObject bJson) { 
    long difference = 0 ; 
    try { 
     String aTime = aJson.getString("time_utc_8"); 
     String bTime = bJson.getString("time_utc_8"); 

     String pattern = "yyyy-MM-dd HH:mm:ss.SSSSSS"; 
     DateTimeFormatter Parser = DateTimeFormatter.ofPattern(pattern).ISO_DATE; 

     System.out.println(aTime); 

     ZonedDateTime a = ZonedDateTime.parse(aTime, Parser); 
     ZonedDateTime b = ZonedDateTime.parse(bTime, Parser); 

     ChronoUnit unit = null; 
     difference = unit.between(a, b); 

     System.out.println(difference); 

    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    String t = difference +""; 
    return t; 

} 

のようなJSONからZonedDateTimeに文字列を変換しようと常に

Exception in thread "main" java.time.format.DateTimeParseException: Text '2016-06-28 22:29:44.700228' could not be parsed: Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO resolved to 2016-06-28T22:29:44.700228 of type java.time.format.Parsed 
at java.time.format.DateTimeFormatter.createError(Unknown Source) 
at java.time.format.DateTimeFormatter.parse(Unknown Source) 
at java.time.OffsetDateTime.parse(Unknown Source) 
at Q2.getWatchTime(Q2.java:321) 
at Q2.WatchTime(Q2.java:265) 
at Q2.main(Q2.java:31) 

エラーを取得し、私はこれらの違いを取得したいです2つの日付。 私はSimpleDateFormatを試しましたが、ミルのエラー結果が表​​示されます。

+3

'Parser'を作成し、すぐに' ISO_DATE'を呼び出すのはあまり意味がありません。 –

+0

は 'Parser'と' ISO_DATE'を持たず、DateTimePareseExceptionも取得します。 –

+1

私が言っていることは、 'DateTimeFormatter.ofPattern(pattern).ISO_DATE'は' DateTimeFormatter.ISO_DATE'に相当するということです。 –

答えて

3

私はすでにコメントのすべてであると思うので、これは要約するだけです。

(1)フォーマットパターン文字列が正しい。あなただけの次の行から.ISO_DATEを削除する必要があり、それは次のようになります。「:00 2011-12-03 + 01」または「2011-12-03」、日付のない

  DateTimeFormatter Parser = DateTimeFormatter.ofPattern(pattern); 

ISO_DATEは、例えば受け入れUTCとのオフセットの有無にかかわらず、ここでは何もわかりません)

(2)文字列にタイムゾーンもオフセットもないようですので、LocalDateTime

を使用してください
  LocalDateTime a = LocalDateTime.parse(aTime, Parser); 
      LocalDateTime b = LocalDateTime.parse(bTime, Parser); 

夏時間(DST)などを取る必要がある場合は、i NTOアカウントは、差を計算する際に、解析後の時間を変換:あなたが期待する結果を得ることが確実であるよう

  ZoneId timeZone = ZoneId.systemDefault(); 
      ZonedDateTime a = LocalDateTime.parse(aTime, Parser).atZone(timeZone); 
      ZonedDateTime b = LocalDateTime.parse(bTime, Parser).atZone(timeZone); 

変換に使用するタイムゾーン考え直すしてください。

(3)ChronoUnitnullは機能しません。私はあなたが意図した1かわからないので、このオプションはランダムではなく、選択されます:

  ChronoUnit unit = ChronoUnit.DAYS; 

これら三つであなたの方法は、自分のコンピュータ上でうまく実行変わります。 1回の実行では、印刷:

2016-06-28 22:29:44.700228 
365 

は、同じ実行では、それは365の文字列を返しました。

1

は私が最終的に

static String getWatchTime(JSONObject aJson, JSONObject bJson) { 
    double difference = 0 ; 
    try { 
     String aTime = aJson.getString("time_utc_8"); 
     String bTime = bJson.getString("time_utc_8"); 

      String pattern = "yyyy-MM-dd HH:mm:ss.SSSSSS"; 
      DateTimeFormatter Parser = DateTimeFormatter.ofPattern(pattern).withZone(ZoneId.systemDefault()); 

      System.out.println(aTime); 


      ZonedDateTime a = ZonedDateTime.parse(aTime,Parser); 
      ZonedDateTime b = ZonedDateTime.parse(bTime,Parser); 

      System.out.println(a); 
      System.out.println(b); 
      //ChronoUnit unit = null ; 
      difference = ChronoUnit.MICROS.between(a, b); 


    } catch (JSONException e) { 
     e.printStackTrace(); 
    } /*catch (ParseException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    }*/ 

    String t = difference +""; 
    return t; 

} 

が、私はタイムゾーンを設定しなかった私の目標を達成するために、このコードを使用し (コメントで)アンドレアスによって答えを得るため、ZonedDateTimeに文字列として入力を変換することはできません。 そして、私はマイクロ秒を取得する必要があるので、私は使用するChronoUnit.MICROS.between() 回答ありがとう

関連する問題