java json日期格式_java – 转换JSON日期格式

我认为第一个数字(1325134800000)是自纪元以来的毫秒数,-0500是时区.这似乎是下面的示例代码的情况,这似乎做你想要的.

以下代码使用Jackson解析JSON输入,如果您还没有选择JSON解析库,我建议使用它.它缺乏错误检查等.

示例代码:

public final class Foo

{

public static void main(final String... args)

throws IOException

{

// What the JSON value must match exactly

// Not anchored since it will be used with the (misnamed) .matches() method

final Pattern pattern

= Pattern.compile("\\\\/Date\\((\\d+)(-\\d+)?\\)\\\\/");

final ObjectMapper mapper = new ObjectMapper();

// Parse JSON...

final JsonNode node = mapper.readTree(

"{\"PostingDate\": \"\\/Date(1325134800000-0500)\\/\"}");

if (!node.has("PostingDate")) {

System.err.println("Bad JSON input!");

System.exit(1);

}

// Get relevant field

final String dateSpec = node.get("PostingDate").getTextValue();

// Try and match the input.

final Matcher matcher = pattern.matcher(dateSpec);

if (!matcher.matches()) {

System.err.println("Bad pattern!"); // Yuck

System.exit(1);

}

// The first group capture the milliseconds, the second one the time zone

final long millis = Long.parseLong(matcher.group(1));

String tz = matcher.group(2);

if (tz.isEmpty()) // It can happen, in which case the default is assumed to be...

tz = "+0000";

// Instantiate a date object...

final Date date = new Date(millis);

// And print it using an appropriate date format

System.out.printf("Date: %s %s\n",

new SimpleDateFormat("yyyy/MM/dd HH:MM:ss").format(date), tz);

}

}

输出:

Date: 2011/12/29 06:12:00 -0500

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值