2016-05-29 9 views
-3

私のObjectMapper日付を変換できません。年月には誤りがあります。たとえば:出力JSON文字列が正しくありません。日付のエラー

example

マイコード::私は2016年5月29日を送っ

{"idtests":null,"title":"sda","date":"3916-06-29T09:27:48"} 

@FXML 
private void AddTestAction(){ 
    if(!title.getText().isEmpty() && date.getValue() != null && minutes.getValue() != null && hours.getValue() != null){ 
      ObjectMapper mapper = new ObjectMapper(); 
      Tests obj = new Tests(); 
      String json = null; 

      obj.setTitle(title.getText()); 
      DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); 
      df.setTimeZone(TimeZone.getTimeZone("CET"));   

      Date t = new Date(); 
      t.setMinutes(Integer.parseInt(minutes.getValue().toString())); 
      t.setHours(Integer.parseInt(hours.getValue().toString())); 
      t.setYear(Integer.parseInt(date.getValue().toString().substring(0,4))); 
      t.setMonth(Integer.parseInt(date.getValue().toString().substring(5,7))); 
      t.setDate(Integer.parseInt(date.getValue().toString().substring(8,10)));   
      obj.setDate(t);     

      try { 
       mapper.setDateFormat(df); 
       mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); 
       json = mapper.writeValueAsString(obj); 
      } catch (JsonProcessingException ex) { 
       Logger.getLogger(adminController.class.getName()).log(Level.SEVERE, null, ex); 
      } 
      System.out.print(json); 
      if(sendMessages("http://localhost:8080/Server/source/tests/", json)){ 
       info.setText("Test został dodany"); 
      }else{ 
       info.setText("Błąd połączenia"); 
      } 

    }else{ 
     info.setText("Wprowadź identyfikator"); 
    } 
} 

すべて提出されたデータが正しいです。それは私にはエラーObjectMapperの構成にする必要がありますがどこですか? 私は1年の間に1900を引いて1ヶ月を引くことができますが、それは簡単すぎます。

答えて

0

Date#setYearを参照してください:

推奨されていません。JDKバージョン1.1以降は、Calendar.set(Calendar.YEAR、year + 1900)に置き換えられました。

[...]

は、指定された値プラス1900

Date#setMonthになるこのDateオブジェクトの年を設定します:

推奨されていません。JDKバージョン1.1以降は、Calendar.set(Calendar.MONTH、int month)に置き換えられました。

[...]

month - 0-11間の月の値。

関連する問題