且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将Json日期转换为java日期

更新时间:2023-11-29 12:23:40

JSONObject store = new JSONObject(response);
if(store.has("CreatedOn")) {
  Timestamp stamp = new Timestamp(store.getLong("CreatedOn"));
  Date date = new Date(stamp.getTime());
  System.out.println(date);
}

JSONObject store = new JSONObject(response);
if(store.has("CreatedOn")) {
Integer datetimestamp = Integer.parseInt(store.getString("CreatedOn").replaceAll("\\D", ""));
 Date date = new Date(datetimestamp);
 DateFormat formatter = new SimpleDateFormat("HH:mm:ss:SSS");
 String dateFormatted = formatter.format(date);
}

考虑使用JSON方法而不是包含。 JSON有has(),如果键存在,则验证。

consider using JSON methods instead of contains. JSON has "has()" which validate if key exists.

您还应确保尝试{}首先捕获{}该String,以确保其有效JSON。

You should also make sure that you try {} catch {} the String first, to make sure its valid JSON.

更新:

您的价值是
/日期(1406192939581)/

Your Value is /Date(1406192939581)/

这意味着它必须先被格式化。
通过使用

which means it must be formatted first. Get it by parsing the string with

Integer datetimestamp = Integer.parseInt(store.getString("CreatedOn").replaceAll("\\D", ""));