From 0101bc0a81071705aca8e94fde7d96836bdce7ab Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 3 Dec 2017 17:34:29 +0100 Subject: [PATCH] parse more iso formats --- .../java/info/nightscout/utils/DateUtil.java | 45 +++++++++++++------ .../info/nightscout/utils/DateUtilTest.java | 1 + 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/info/nightscout/utils/DateUtil.java b/app/src/main/java/info/nightscout/utils/DateUtil.java index d2f35e0c99..c406568730 100644 --- a/app/src/main/java/info/nightscout/utils/DateUtil.java +++ b/app/src/main/java/info/nightscout/utils/DateUtil.java @@ -29,6 +29,7 @@ public class DateUtil { */ private static String FORMAT_DATE_ISO = "yyyy-MM-dd'T'HH:mm:ssZ"; private static String FORMAT_DATE_ISO_MSEC = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; + private static String FORMAT_DATE_ISO_MSEC_UTC = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; /** * Takes in an ISO date string of the following format: @@ -42,15 +43,31 @@ public class DateUtil { throws Exception { SimpleDateFormat f = new SimpleDateFormat(FORMAT_DATE_ISO, Locale.getDefault()); Date date; + f.setTimeZone(TimeZone.getTimeZone("UTC")); try { date = f.parse(isoDateString); + return date; } catch (ParseException e) { - f = new SimpleDateFormat(FORMAT_DATE_ISO_MSEC, Locale.getDefault()); - f.setTimeZone(TimeZone.getTimeZone("UTC")); - date = f.parse(isoDateString); } - return date; + + f = new SimpleDateFormat(FORMAT_DATE_ISO_MSEC, Locale.getDefault()); + f.setTimeZone(TimeZone.getTimeZone("UTC")); + try { + date = f.parse(isoDateString); + return date; + } catch (ParseException e) { + } + + f = new SimpleDateFormat(FORMAT_DATE_ISO_MSEC_UTC, Locale.getDefault()); + f.setTimeZone(TimeZone.getTimeZone("UTC")); + try { + date = f.parse(isoDateString); + return date; + } catch (ParseException e) { + } + + throw new ParseException("Unparseable date: " + isoDateString, 0); } /** @@ -72,6 +89,7 @@ public class DateUtil { public static String toISOString(Date date) { return toISOString(date, FORMAT_DATE_ISO, TimeZone.getTimeZone("UTC")); } + public static String toISOString(long date) { return toISOString(new Date(date), FORMAT_DATE_ISO, TimeZone.getTimeZone("UTC")); } @@ -125,6 +143,7 @@ public class DateUtil { public static String dateAndTimeString(Date date) { return dateString(date) + " " + timeString(date); } + public static String dateAndTimeString(long mills) { return dateString(mills) + " " + timeString(mills); } @@ -146,19 +165,19 @@ public class DateUtil { } - public static String timeFrameString(long timeInMillis){ - long remainingTimeMinutes = timeInMillis/(1000*60); - long remainingTimeHours = remainingTimeMinutes/60; - remainingTimeMinutes = remainingTimeMinutes%60; - return "(" + ((remainingTimeHours >0)?(remainingTimeHours + "h "):"") + remainingTimeMinutes + "')"; + public static String timeFrameString(long timeInMillis) { + long remainingTimeMinutes = timeInMillis / (1000 * 60); + long remainingTimeHours = remainingTimeMinutes / 60; + remainingTimeMinutes = remainingTimeMinutes % 60; + return "(" + ((remainingTimeHours > 0) ? (remainingTimeHours + "h ") : "") + remainingTimeMinutes + "')"; } - public static String sinceString(long timestamp){ - return timeFrameString(System.currentTimeMillis()-timestamp); + public static String sinceString(long timestamp) { + return timeFrameString(System.currentTimeMillis() - timestamp); } - public static String untilString(long timestamp){ - return timeFrameString(timestamp- System.currentTimeMillis()); + public static String untilString(long timestamp) { + return timeFrameString(timestamp - System.currentTimeMillis()); } diff --git a/app/src/test/java/info/nightscout/utils/DateUtilTest.java b/app/src/test/java/info/nightscout/utils/DateUtilTest.java index f269253ba8..4b48148341 100644 --- a/app/src/test/java/info/nightscout/utils/DateUtilTest.java +++ b/app/src/test/java/info/nightscout/utils/DateUtilTest.java @@ -19,6 +19,7 @@ public class DateUtilTest { public void fromISODateStringTest() throws Exception { assertEquals( 1511124634417L, DateUtil.fromISODateString("2017-11-19T22:50:34.417+0200").getTime()); assertEquals( 1511124634000L, DateUtil.fromISODateString("2017-11-19T22:50:34+0200").getTime()); + assertEquals( 1512317365000L, DateUtil.fromISODateString("2017-12-03T16:09:25.000Z").getTime()); }