.net JSON Date Format

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

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

thanks

+1
source share
3 answers

I assume the timestamp is in UTC, and the offset is the UTC offset of the desired local time. If the timestamp is at a given offset from UTC, you will have to generate it a little differently.

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

 // 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 is a little annoying that DateTimeFormat not able to output milliseconds from an era; this is what dt.getMillis() concatenation requires.

To generate the same using 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()) +")/"; 

This is essentially the same as the Joda-Time example, but the bit in which you need to copy the time zone from the calendar to the date format is the main source of errors.

+3
source

The second number simply indicates that the DateTime value should be interpreted as local time (instead of UTC), the number itself is ignored. This is described in the additional information / date format section of the document format at http://msdn.microsoft.com/en-us/library/bb412170.aspx .

+2
source

The question is a bit outdated, but I think there are still people banging their heads against the wall because of this excellent choice of date format;)

An adapter of type Serialization and deserialisation of a DateTime date value in .NET Json format in Java for Gson This worked for me now, starting from about two years old.

 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 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) + ")/"); } } 
+2
source

All Articles