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

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

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

问题

以下是翻译好的部分:

{
   "monTableau": [
      {
         "Données": "ONE",
         "Date import": "20180130000000",
         "Date export": "20180130000000"
      },
      {
         "Données": "TWO",
         "Date import": "20190101000000",
         "Date export": "20190101000000"
      },
      {
         "Données": "THREE",
         "Date import": "20200101000000",
         "Date export": "20200101000000"
      },
      {
         "Données": "FOUR",
         "Date import": "20180130000000",
         "Date export": "20180130000000"
      }
   ]
}
public class App 
{
    public static void main( String[] args )
    {
        JSONParser jsonP = new JSONParser();
        SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yy");

        try {
            JSONObject jsonO = (JSONObject) jsonP.parse(new FileReader("C:/myJsonTest.json"));
            JSONArray myArray = (JSONArray) jsonO.get("monTableau");

            for (int i=0;i<myArray.size();i++){
                JSONObject jo = (JSONObject) myArray.get(i);
                String donnees = (String) jo.get("Données");
                String dateImport = (String) jo.get("Date import");
                Date date = df.parse(dateImport);
                String dateExport = (String) jo.get("Date export");
                System.out.println("Donnees : "+donnees);
                //System.out.println("Date import : "+date);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
    }
}
英文:

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 :

{
   &quot;monTableau&quot;: [
      {
         &quot;Donn&#233;es&quot;: &quot;ONE&quot;,
         &quot;Date import&quot;: &quot;20180130000000&quot;,
         &quot;Date export&quot;: &quot;20180130000000&quot;
      },
      {
         &quot;Donn&#233;es&quot;: &quot;TWO&quot;,
         &quot;Date import&quot;: &quot;20190101000000&quot;,
         &quot;Date export&quot;: &quot;20190101000000&quot;
      },
      {
         &quot;Donn&#233;es&quot;: &quot;THREE&quot;,
         &quot;Date import&quot;: &quot;20200101000000&quot;,
         &quot;Date export&quot;: &quot;20200101000000&quot;
      },
      {
         &quot;Donn&#233;es&quot;: &quot;FOUR&quot;,
         &quot;Date import&quot;: &quot;20180130000000&quot;,
         &quot;Date export&quot;: &quot;20180130000000&quot;
      }
   ]
}
public class App 
{
    public static void main( String[] args )
    {
        JSONParser jsonP = new JSONParser();
        SimpleDateFormat df = new SimpleDateFormat(&quot;dd-MMM-yy&quot;);

        try {
            JSONObject jsonO = (JSONObject) jsonP.parse(new FileReader(&quot;C:/myJsonTest.json&quot;));
            JSONArray myArray = (JSONArray) jsonO.get(&quot;monTableau&quot;);

            for (int i=0;i&lt;myArray.size();i ++){
                JSONObject jo = (JSONObject) myArray.get(i);
                String donnees = (String) jo.get(&quot;Donn&#233;es&quot;);
                String dateImport = (String) jo.get(&quot;Date import&quot;);
                Date date = df.parse(dateImport);
                String dateExport = (String) jo.get(&quot;Date export&quot;);
                System.out.println(&quot;Donnees : &quot;+donnees);
                //System.out.println(&quot;Date import : &quot;+date);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
    }
}

答案1

得分: 1

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

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

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:

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:

确定