.NET JSON日期格式 [英] .net JSON Date format

查看:196
本文介绍了.NET JSON日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于来自.NET服务的响应,我得到这个日期格式: /日(1233323754523 + 0100)/

As response from .net service I get this date format: /Date(1233323754523+0100)/

1233323754523是日期时间戳格式,但我不知道是什么意思+0100以及如何从Java code产生的?

1233323754523 is date in timestamp format, but I don't know what +0100 mean and how to generate this from java code?

感谢

推荐答案

我假定时间戳是UTC的偏移量是UTC的预期本地时间偏移。如果时间戳是UTC的给定偏移量,你就必须产生稍微不同。

I assume that the timestamp is in UTC and the offset is the UTC offset of the desired local time. If the timestamp is in the given offset from UTC, you'd have to generate it slightly differently.

一个可靠的方式来产生这在Java中会使用乔达时库,它是比德默认好得多 java.util.Date 的java.util.Calendar 类。

A reliable way to generate this in Java would be using the Joda-Time library, which is much better than de default java.util.Date and java.util.Calendar classes.

// A formatter that prints the timezone offset
DateTimeFormatter fmt = DateTimeFormat.forPattern("Z");

// The current date+time in the system default timezone.
DateTime dt = new DateTime();

// Create the result.
String result = "/Date(" + dt.getMillis() + fmt.print(dt) + ")/";

这是一个有点遗憾的是, DateTimeFormat 没有办法输出,因为时代的毫秒;那是什么就必须对 dt.getMillis()字符串连接。

要使用 java.util中的班会是这个样子产生了同样的事情:

To generate the same thing using the java.util classes would look something like this:

// A formatter that prints the timezone offset
SimpleDateFormat df = new SimpleDateFormat("Z");    

// Current date+time in system default timezone.
Calendar now = Calendar.getInstance();

// Don't forget this if you use a timezone other than system default:
df.setTimeZone( now.getTimeZone() );

// Create the result
String result = "/Date(" now.getTimeInMillis() + df.format(now.getTime()) +")/";

这是本质上是相同的乔达时的例子,但是,你必须从日历到日期格式复制的时区位是错误的主要来源。

It's essentially the same as the Joda-Time example, but the bit where you have to copy the time zone from the calendar into the date formatter is a major source of bugs.

这篇关于.NET JSON日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆