Merge pull request #2302 from mfietz/issue/2269-incorrect-weekdays

When date string parsing failed, try parsing the string without the weekday
This commit is contained in:
Martin Fietz 2017-04-16 20:09:04 +02:00 committed by GitHub
commit 9542ef1569
2 changed files with 17 additions and 4 deletions

View File

@ -86,7 +86,7 @@ public class DateUtilsTest extends AndroidTestCase {
}
public void testAsctime() throws Exception {
GregorianCalendar exp = new GregorianCalendar(2011, 4, 25, 12, 33, 00);
GregorianCalendar exp = new GregorianCalendar(2011, 4, 25, 12, 33, 0);
exp.setTimeZone(TimeZone.getTimeZone("UTC"));
Date expected = new Date(exp.getTimeInMillis());
Date actual = DateUtils.parse("Wed, 25 May 2011 12:33:00");
@ -102,7 +102,7 @@ public class DateUtilsTest extends AndroidTestCase {
}
public void testParseDateWithNoTimezonePadding() throws Exception {
GregorianCalendar exp = new GregorianCalendar(2017, 1, 22, 22, 28, 00);
GregorianCalendar exp = new GregorianCalendar(2017, 1, 22, 22, 28, 0);
exp.setTimeZone(TimeZone.getTimeZone("UTC"));
Date expected = new Date(exp.getTimeInMillis() + 2);
Date actual = DateUtils.parse("2017-02-22T14:28:00.002-08:00");
@ -110,16 +110,24 @@ public class DateUtilsTest extends AndroidTestCase {
}
public void testParseDateWithForCest() throws Exception {
GregorianCalendar exp1 = new GregorianCalendar(2017, 0, 28, 22, 00, 00);
GregorianCalendar exp1 = new GregorianCalendar(2017, 0, 28, 22, 0, 0);
exp1.setTimeZone(TimeZone.getTimeZone("UTC"));
Date expected1 = new Date(exp1.getTimeInMillis());
Date actual1 = DateUtils.parse("Sun, 29 Jan 2017 00:00:00 CEST");
assertEquals(expected1, actual1);
GregorianCalendar exp2 = new GregorianCalendar(2017, 0, 28, 23, 00, 00);
GregorianCalendar exp2 = new GregorianCalendar(2017, 0, 28, 23, 0, 0);
exp2.setTimeZone(TimeZone.getTimeZone("UTC"));
Date expected2 = new Date(exp2.getTimeInMillis());
Date actual2 = DateUtils.parse("Sun, 29 Jan 2017 00:00:00 CET");
assertEquals(expected2, actual2);
}
public void testParseDateWithIncorrectWeekday() {
GregorianCalendar exp1 = new GregorianCalendar(2014, 9, 8, 9, 0, 0);
exp1.setTimeZone(TimeZone.getTimeZone("GMT"));
Date expected = new Date(exp1.getTimeInMillis());
Date actual = DateUtils.parse("Thu, 8 Oct 2014 09:00:00 GMT"); // actually a Wednesday
assertEquals(expected, actual);
}
}

View File

@ -103,6 +103,11 @@ public class DateUtils {
}
}
// if date string starts with a weekday, try parsing date string without it
if(date.matches("^\\w+, .*$")) {
return parse(date.substring(date.indexOf(',') + 1));
}
Log.d(TAG, "Could not parse date string \"" + input + "\" [" + date + "]");
return null;
}