Unparseable date : “20180……” Json date to java

huangapple go评论196阅读模式
英文:

Unparseable date : "20180......" Json date to java

问题

以下是翻译好的部分:

  1. {
  2. "monTableau": [
  3. {
  4. "Données": "ONE",
  5. "Date import": "20180130000000",
  6. "Date export": "20180130000000"
  7. },
  8. {
  9. "Données": "TWO",
  10. "Date import": "20190101000000",
  11. "Date export": "20190101000000"
  12. },
  13. {
  14. "Données": "THREE",
  15. "Date import": "20200101000000",
  16. "Date export": "20200101000000"
  17. },
  18. {
  19. "Données": "FOUR",
  20. "Date import": "20180130000000",
  21. "Date export": "20180130000000"
  22. }
  23. ]
  24. }
  1. public class App
  2. {
  3. public static void main( String[] args )
  4. {
  5. JSONParser jsonP = new JSONParser();
  6. SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yy");
  7. try {
  8. JSONObject jsonO = (JSONObject) jsonP.parse(new FileReader("C:/myJsonTest.json"));
  9. JSONArray myArray = (JSONArray) jsonO.get("monTableau");
  10. for (int i=0;i<myArray.size();i++){
  11. JSONObject jo = (JSONObject) myArray.get(i);
  12. String donnees = (String) jo.get("Données");
  13. String dateImport = (String) jo.get("Date import");
  14. Date date = df.parse(dateImport);
  15. String dateExport = (String) jo.get("Date export");
  16. System.out.println("Donnees : "+donnees);
  17. //System.out.println("Date import : "+date);
  18. }
  19. } catch (ParseException e) {
  20. e.printStackTrace();
  21. } catch (FileNotFoundException e) {
  22. e.printStackTrace();
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. } catch (java.text.ParseException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. }
英文:

I'm trying to parse a date in json format like this &quot;20180130000000&quot;,
and the program sends me this exception do you have any idea how to do this?
Here is my code :

  1. {
  2. &quot;monTableau&quot;: [
  3. {
  4. &quot;Donn&#233;es&quot;: &quot;ONE&quot;,
  5. &quot;Date import&quot;: &quot;20180130000000&quot;,
  6. &quot;Date export&quot;: &quot;20180130000000&quot;
  7. },
  8. {
  9. &quot;Donn&#233;es&quot;: &quot;TWO&quot;,
  10. &quot;Date import&quot;: &quot;20190101000000&quot;,
  11. &quot;Date export&quot;: &quot;20190101000000&quot;
  12. },
  13. {
  14. &quot;Donn&#233;es&quot;: &quot;THREE&quot;,
  15. &quot;Date import&quot;: &quot;20200101000000&quot;,
  16. &quot;Date export&quot;: &quot;20200101000000&quot;
  17. },
  18. {
  19. &quot;Donn&#233;es&quot;: &quot;FOUR&quot;,
  20. &quot;Date import&quot;: &quot;20180130000000&quot;,
  21. &quot;Date export&quot;: &quot;20180130000000&quot;
  22. }
  23. ]
  24. }
  1. public class App
  2. {
  3. public static void main( String[] args )
  4. {
  5. JSONParser jsonP = new JSONParser();
  6. SimpleDateFormat df = new SimpleDateFormat(&quot;dd-MMM-yy&quot;);
  7. try {
  8. JSONObject jsonO = (JSONObject) jsonP.parse(new FileReader(&quot;C:/myJsonTest.json&quot;));
  9. JSONArray myArray = (JSONArray) jsonO.get(&quot;monTableau&quot;);
  10. for (int i=0;i&lt;myArray.size();i ++){
  11. JSONObject jo = (JSONObject) myArray.get(i);
  12. String donnees = (String) jo.get(&quot;Donn&#233;es&quot;);
  13. String dateImport = (String) jo.get(&quot;Date import&quot;);
  14. Date date = df.parse(dateImport);
  15. String dateExport = (String) jo.get(&quot;Date export&quot;);
  16. System.out.println(&quot;Donnees : &quot;+donnees);
  17. //System.out.println(&quot;Date import : &quot;+date);
  18. }
  19. } catch (ParseException e) {
  20. e.printStackTrace();
  21. } catch (FileNotFoundException e) {
  22. e.printStackTrace();
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. } catch (java.text.ParseException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. }

答案1

得分: 1

你使用的日期格式化程序模式是 dd-MMM-yy。但是你的输入完全不同。你需要根据你的输入调整模式。

尝试使用不同的模式,例如:

  1. SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
英文:

You defined your date formatter with the pattern dd-MMM-yy. But your input it completely different. You have to adjust the pattern according to your input.

Try a different pattern like:

  1. SimpleDateFormat df = new SimpleDateFormat(&quot;yyyyMMddHHmmss&quot;);

huangapple
  • 本文由 发表于 2020年10月8日 17:46:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/64259896.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定