Created
April 17, 2012 17:42
-
-
Save devnulled/2407714 to your computer and use it in GitHub Desktop.
Parse a Microsoft .NET style JSON Date/Timestamp into a Java Date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package devnulled; | |
| import java.util.Date; | |
| import java.util.regex.Matcher; | |
| import java.util.regex.Pattern; | |
| public class DateTimeUtils { | |
| /** | |
| * <p> | |
| * Parses a Microsoft .NET style JSON timestamp and returns a Java Date irrespective of time zones (but can parse them) | |
| * </p> | |
| * | |
| * <a href="http://weblogs.asp.net/bleroy/archive/2008/01/18/dates-and-json.aspx">Microsoft .NET JSON Format Reference</a> | |
| * <a href="http://benjii.me/2010/04/deserializing-json-in-android-using-gson/">GSON Code Reference</a> | |
| * | |
| * @param msJsonDateTime The String representation of a Microsoft style timestamp | |
| * @return Java Date that represents the timestamp | |
| */ | |
| public static Date parseMsTimestampToDate(final String msJsonDateTime) { | |
| if(msJsonDateTime == null) return null; | |
| String JSONDateToMilliseconds = "\\\\/(Date\\\\((-*.*?)([\\\\+\\\\-].*)?\\\\))\\\\/"; | |
| Pattern pattern = Pattern.compile(JSONDateToMilliseconds); | |
| Matcher matcher = pattern.matcher(msJsonDateTime); | |
| String ts = matcher.replaceAll("$2"); | |
| Date retValue = new Date(new Long(ts)); | |
| return retValue; | |
| } | |
| } |
how to get solution for that ?
this should work
String JSONDateToMilliseconds = "\/(Date\((-.?)([\+\-].*)?\))\/";
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not working =/
Try: /Date(1395305220000-0300)/