diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/com/isode/stroke/base/DateTime.java | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/src/com/isode/stroke/base/DateTime.java b/src/com/isode/stroke/base/DateTime.java index 4c8310a..3cceb09 100644 --- a/src/com/isode/stroke/base/DateTime.java +++ b/src/com/isode/stroke/base/DateTime.java @@ -12,13 +12,34 @@ import java.util.Locale; import java.util.TimeZone; public class DateTime { + + // Cannot use Java 7 'X' format specifier for timezone + // (not supported on Android) public static Date stringToDate(String date) { - SimpleDateFormat parser = new SimpleDateFormat(format, Locale.US); + if (date == null || date.length() < 1) return null; + SimpleDateFormat parser; + if (date.charAt(date.length()-1) == 'Z') { + parser = new SimpleDateFormat(parseFormatZ, Locale.US); + } else { + parser = new SimpleDateFormat(parseFormatTz, Locale.US); + } parser.setTimeZone(TimeZone.getTimeZone("UTC")); try { return parser.parse(date); } catch (ParseException e) { - return null; + if (date.matches(".*[+-]\\d\\d:\\d\\d")) { // strip ':' from timezone + date = date.substring(0, date.length()-3) + date.substring(date.length()-2); + } else if (date.matches(".*[+-]\\d\\d")) { // make timezone 4 digits (append minutes) + date += "00"; + } else { + parser = new SimpleDateFormat(parseFormatNoTz, Locale.US); + parser.setTimeZone(TimeZone.getTimeZone("UTC")); + } + try { + return parser.parse(date); + } catch (ParseException e1) { + return null; + } } } @@ -28,5 +49,8 @@ public class DateTime { return parser.format(date); } - static private String format = "yyyy-MM-dd'T'HH:mm:ssXXX"; + static private String format = "yyyy-MM-dd'T'HH:mm:ss'Z'"; + static private String parseFormatTz = "yyyy-MM-dd'T'HH:mm:ssZ"; + static private String parseFormatZ = "yyyy-MM-dd'T'HH:mm:ss'Z'"; + static private String parseFormatNoTz = "yyyy-MM-dd'T'HH:mm:ss"; } |