From ed76fffc30d9ca2e180e56dc5a6f23f49c95cb53 Mon Sep 17 00:00:00 2001 From: Martin Fietz Date: Sat, 18 Apr 2015 13:12:01 +0200 Subject: [PATCH] Improve patterns Hyphen can be used as a separator between day, month and date, but also to indicate a negative time zone. --- .../antennapod/core/util/DateUtilsTest.java | 16 +++++ .../antennapod/core/util/DateUtils.java | 59 +++++-------------- 2 files changed, 31 insertions(+), 44 deletions(-) diff --git a/core/src/androidTest/java/de/danoeh/antennapod/core/util/DateUtilsTest.java b/core/src/androidTest/java/de/danoeh/antennapod/core/util/DateUtilsTest.java index cca753895..46140fbd1 100644 --- a/core/src/androidTest/java/de/danoeh/antennapod/core/util/DateUtilsTest.java +++ b/core/src/androidTest/java/de/danoeh/antennapod/core/util/DateUtilsTest.java @@ -56,4 +56,20 @@ public class DateUtilsTest extends AndroidTestCase { assertEquals(900, actual.getTime()%1000); } + public void testParseDateWithTimezoneName() throws Exception { + GregorianCalendar exp = new GregorianCalendar(2015, 2, 28, 6, 31, 4); + exp.setTimeZone(TimeZone.getTimeZone("UTC")); + Date expected = new Date(exp.getTimeInMillis()); + Date actual = DateUtils.parse("Sat, 28 Mar 2015 01:31:04 EST"); + assertEquals(expected, actual); + } + + public void testParseDateWithTimeZoneOffset() throws Exception { + GregorianCalendar exp = new GregorianCalendar(2015, 2, 28, 12, 16, 12); + exp.setTimeZone(TimeZone.getTimeZone("UTC")); + Date expected = new Date(exp.getTimeInMillis()); + Date actual = DateUtils.parse("Sat, 28 March 2015 08:16:12 -0400"); + assertEquals(expected, actual); + } + } diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/DateUtils.java b/core/src/main/java/de/danoeh/antennapod/core/util/DateUtils.java index fa41fb664..b6df2dc85 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/util/DateUtils.java +++ b/core/src/main/java/de/danoeh/antennapod/core/util/DateUtils.java @@ -13,45 +13,14 @@ import java.util.Locale; * Parses several date formats. */ public class DateUtils { - private static final String TAG = "DateUtils"; + + private static final String TAG = "DateUtils"; - private static final String[] RFC822DATES = {"dd MMM yy HH:mm:ss Z", - "dd MMM yy HH:mm Z"}; - - /** - * RFC 3339 date format for UTC dates. - */ - public static final String RFC3339UTC = "yyyy-MM-dd'T'HH:mm:ss'Z'"; - - /** - * RFC 3339 date format for localtime dates with offset. - */ - public static final String RFC3339LOCAL = "yyyy-MM-dd'T'HH:mm:ssZ"; - - public static final String ISO8601_SHORT = "yyyy-MM-dd"; - - private static ThreadLocal RFC822Formatter = new ThreadLocal() { - @Override - protected SimpleDateFormat initialValue() { - return new SimpleDateFormat("dd MMM yy HH:mm:ss Z", Locale.US); - } - - }; - - private static ThreadLocal RFC3339Formatter = new ThreadLocal() { - @Override - protected SimpleDateFormat initialValue() { - return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US); - } - - }; - - public static Date parse(String date) { - if(date == null) { + public static Date parse(final String input) { + if(input == null) { throw new IllegalArgumentException("Date most not be null"); } - date = date.replace('/', ' '); - date = date.replace('-', ' '); + String date = input.replace('/', '-'); if(date.contains(".")) { int start = date.indexOf('.'); int current = start+1; @@ -77,15 +46,16 @@ public class DateUtils { "dd MMM yy HH:mm:ss Z", "dd MMM yy HH:mm Z", "EEE, dd MMM yyyy HH:mm:ss Z", + "EEE, dd MMMM yyyy HH:mm:ss Z", "EEEE, dd MMM yy HH:mm:ss Z", "EEE MMM d HH:mm:ss yyyy", - "yyyy MM dd'T'HH:mm:ss", - "yyyy MM dd'T'HH:mm:ss.SSS", - "yyyy MM dd'T'HH:mm:ss.SSS Z", - "yyyy MM dd'T'HH:mm:ssZ", - "yyyy MM dd'T'HH:mm:ss'Z'", - "yyyy MM ddZ", - "yyyy MM dd" + "yyyy-MM-dd'T'HH:mm:ss", + "yyyy-MM-dd'T'HH:mm:ss.SSS", + "yyyy-MM-dd'T'HH:mm:ss.SSS Z", + "yyyy-MM-dd'T'HH:mm:ssZ", + "yyyy-MM-dd'T'HH:mm:ss'Z'", + "yyyy-MM-ddZ", + "yyyy-MM-dd" }; SimpleDateFormat parser = new SimpleDateFormat("", Locale.US); parser.setLenient(false); @@ -98,7 +68,8 @@ public class DateUtils { return result; } } - Log.d(TAG, "Could not parse '" + date + "'"); + + Log.d(TAG, "Could not parse date string \"" + input + "\""); return null; }