summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlan Young <consult.awy@gmail.com>2016-04-18 16:30:24 (GMT)
committerAlan Young <consult.awy@gmail.com>2016-04-20 16:28:48 (GMT)
commit0b78451839eebdba59d8ac603067f5c7c7d5284c (patch)
tree35782ab98f1551cc338662f3d13b4e2ae0a9738d
parent9b518dcdd98d694a4464895fce17a3c7a8caf569 (diff)
downloadstroke-0b78451839eebdba59d8ac603067f5c7c7d5284c.zip
stroke-0b78451839eebdba59d8ac603067f5c7c7d5284c.tar.bz2
Revert use of Java 7 'XXX' Time Pattern element.
Not supported in Android. Was introduced in commit 8fe7526. Change-Id: I9fb396749c92b89cbaa7fac6cdeb047161ed0358
-rw-r--r--src/com/isode/stroke/base/DateTime.java30
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";
}