This file contains 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
import org.joda.time.DateTime; | |
import org.joda.time.DateTimeZone; | |
public class WCFDateTimeParser { | |
/** | |
* <p>WCF services supply Dates over JSON in a strange format. This method | |
* takes a WCF-formatted Date string and parses it into a JodaTime DateTime | |
* object. Assumes valid input matching a format described below.</p> | |
* | |
* <p>WCF Dates over JSON can vary in 3 ways:</p> | |
* <pre> | |
* /Date(946684800000)/ | |
* /Date(-4094535600000+1300)/ | |
* /Date(4094535600000-0330)/</pre> | |
* | |
* <p>That's milliseconds since Jan 1, 1970, plus/minus an optional timezone | |
* (the milliseconds are in UTC, the timezone is applied afterwards). Note | |
* that it is also possible that the first part (milliseconds) may be | |
* negative - that is, a Date that occurred before 1970.</p> | |
*/ | |
public static org.joda.time.DateTime parseWCFDateTimeString(String wcfDate) { | |
// Strip the '/Date(' and ')/' bits off: | |
wcfDate = wcfDate.replace("/Date(", ""); | |
wcfDate = wcfDate.replace(")/", ""); | |
// Find our timezone location within the string. | |
int timezoneStart = wcfDate.indexOf("+"); | |
if (timezoneStart == -1) { | |
timezoneStart = wcfDate.lastIndexOf("-"); | |
} | |
// Our date string doesn't have a timezone attached (0 index for Dates | |
// before 1970 without a timezone). Just return the main part as a | |
// DateTime object - note this will use the default locale. | |
if (timezoneStart == -1 || timezoneStart == 0) { | |
return new DateTime(Long.parseLong(wcfDate)); | |
} | |
String dateTimePart = wcfDate.substring(0, timezoneStart); | |
String timezonePart = wcfDate.substring(timezoneStart); | |
long dateTimeMilliseconds = Long.parseLong(dateTimePart); | |
// Split our timezonePart into hours and minutes | |
// Note parseInt will fail for '+10', but succeed for '10' and '-10'. | |
if (timezonePart.startsWith("+")) { | |
timezonePart = timezonePart.substring(1); | |
} | |
// If our timezonePart is 5 chars long, its form is '-0830'. | |
// If it's 4 chars long, its form is '0830'. | |
int minutesPartIndex = timezonePart.length() == 5 ? 3 : 2; | |
String hoursPart = timezonePart.substring(0, minutesPartIndex); | |
String minutesPart = timezonePart.substring(minutesPartIndex); | |
int offsetHours = Integer.parseInt(hoursPart); | |
int offsetMinutes = Integer.parseInt(minutesPart); | |
// Use JodaTime here. Java's Date class doesn't support instantiation | |
// with a timezone. http://www.joda.org/joda-time/ | |
DateTimeZone timezone = DateTimeZone.forOffsetHoursMinutes(offsetHours, | |
offsetMinutes); | |
DateTime dateTime = new DateTime(dateTimeMilliseconds, timezone); | |
return dateTime; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment