英文:
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 "20180130000000"
,
and the program sends me this exception do you have any idea how to do this?
Here is my code :
{
"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();
}
}
}
答案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("yyyyMMddHHmmss");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论