.net JSON Date format
As response from .net service I get this date format: /Date(1233323754523+0100)/
1233323754523 is date in timestamp format, but I don't know what开发者_如何转开发 +0100 mean and how to generate this from java code?
Thanks
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.
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) + ")/";
It's a bit unfortunate that the DateTimeFormat
does not have a way to output the milliseconds since epoch; that's what necessitates the dt.getMillis()
string concatenation.
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.
The second number simply indicates that the DateTime value should be interpreted as a local date time (instead of UTC), the number itself is ignored. This is described in the Advanced Information / DateTime wire format section of the document at http://msdn.microsoft.com/en-us/library/bb412170.aspx.
The question is a little out of date, but i think there's still some people out there hitting their heads against a wall because of that excellent date time format choice ;)
Serialisation and deserialisation type adapter of a .NET Json formatted DateTime value in Java for Gson This worked for me now since around 2years.
package x
import java.lang.reflect.Type;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.regex.Pattern;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
/**
* Serialises and deserialises a string representing a date produced by a .NET web service.
*
* @author Diego Frehner
*/
public class TypeAdapterDate implements JsonSerializer<Date>, JsonDeserializer<Date> {
/** Pattern for parsing date time values sent by a .NET web service. */
private static Pattern pattern = Pattern.compile("^/Date\\([0-9\\+-]*\\)/$");
private static DecimalFormat formatter = new DecimalFormat("#00.###");
private static final long HOUR_IN_MILLISECOND = 60L * 60 * 1000;
private static final String minusSign = "-";
private static final String plusSign = "+";
@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String value = json.getAsString();
// example .NET web service return value: /Date(1302847200000+0200)/
// milliseconds since midnight 1970 UTC + time zone offset of the server to UTC
// GMT == UTC == UT it's all the same...afaik
if (!pattern.matcher(value).matches()) {
return null;
}
// get UTC millisecond value
long utcMillisecond = Long.parseLong(value.substring(value.indexOf("(") + 1, value.indexOf(")") - 5));
// new Date(long) takes milliseconds since 1970 in UTC
return new Date(utcMillisecond);
}
@Override
public JsonElement serialize(Date arg0, Type arg1, JsonSerializationContext arg2) {
Date date = (Date) arg0;
int zoneOffsetMillisecond = TimeZone.getDefault().getOffset(date.getTime());
String sign = plusSign;
if (zoneOffsetMillisecond < 0) { // negative offset
sign = minusSign;
zoneOffsetMillisecond *= -1;
}
int minute = (int) (zoneOffsetMillisecond % HOUR_IN_MILLISECOND);
int hour = (zoneOffsetMillisecond / 1000 / 60 / 60);
return new JsonPrimitive("/Date(" + date.getTime() + sign + formatter.format(hour) + formatter.format(minute) + ")/");
}
}
精彩评论