Improve patterns

Hyphen can be used as a separator between day, month and date, but also
to indicate a negative time zone.
This commit is contained in:
Martin Fietz 2015-04-18 13:12:01 +02:00 committed by Martin Fietz
parent 73c4dfc04d
commit ed76fffc30
2 changed files with 31 additions and 44 deletions

View File

@ -56,4 +56,20 @@ public class DateUtilsTest extends AndroidTestCase {
assertEquals(900, actual.getTime()%1000); 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);
}
} }

View File

@ -13,45 +13,14 @@ import java.util.Locale;
* Parses several date formats. * Parses several date formats.
*/ */
public class DateUtils { 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", public static Date parse(final String input) {
"dd MMM yy HH:mm Z"}; if(input == null) {
/**
* 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<SimpleDateFormat> RFC822Formatter = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("dd MMM yy HH:mm:ss Z", Locale.US);
}
};
private static ThreadLocal<SimpleDateFormat> RFC3339Formatter = new ThreadLocal<SimpleDateFormat>() {
@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) {
throw new IllegalArgumentException("Date most not be null"); throw new IllegalArgumentException("Date most not be null");
} }
date = date.replace('/', ' '); String date = input.replace('/', '-');
date = date.replace('-', ' ');
if(date.contains(".")) { if(date.contains(".")) {
int start = date.indexOf('.'); int start = date.indexOf('.');
int current = start+1; int current = start+1;
@ -77,15 +46,16 @@ public class DateUtils {
"dd MMM yy HH:mm:ss Z", "dd MMM yy HH:mm:ss Z",
"dd MMM yy HH:mm Z", "dd MMM yy HH:mm Z",
"EEE, dd MMM yyyy HH:mm:ss 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", "EEEE, dd MMM yy HH:mm:ss Z",
"EEE MMM d HH:mm:ss yyyy", "EEE MMM d HH:mm:ss yyyy",
"yyyy MM dd'T'HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ss",
"yyyy MM dd'T'HH:mm:ss.SSS", "yyyy-MM-dd'T'HH:mm:ss.SSS",
"yyyy MM dd'T'HH:mm:ss.SSS Z", "yyyy-MM-dd'T'HH:mm:ss.SSS Z",
"yyyy MM dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:ssZ",
"yyyy MM dd'T'HH:mm:ss'Z'", "yyyy-MM-dd'T'HH:mm:ss'Z'",
"yyyy MM ddZ", "yyyy-MM-ddZ",
"yyyy MM dd" "yyyy-MM-dd"
}; };
SimpleDateFormat parser = new SimpleDateFormat("", Locale.US); SimpleDateFormat parser = new SimpleDateFormat("", Locale.US);
parser.setLenient(false); parser.setLenient(false);
@ -98,7 +68,8 @@ public class DateUtils {
return result; return result;
} }
} }
Log.d(TAG, "Could not parse '" + date + "'");
Log.d(TAG, "Could not parse date string \"" + input + "\"");
return null; return null;
} }