parse Date for list of possible formats
/*
* Copyright (C) 2007-2010 Michael Novak <mike@androidnerds.org>, Josh Guilfoyle <jasta@devtcg.org>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* Code lifted from Informa <http://informa.sourceforge.net>.
*/
//package org.androidnerds.reader.util;
import android.util.Log;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
class DateUtils {
private static final String TAG = "DateUtils";
private static final SimpleDateFormat[] dateFormats;
private static final int defaultFormat = 2;
private DateUtils() { }
static {
final String[] possibleFormats = {
"EEE, dd MMM yyyy HH:mm:ss z", // RFC_822
"EEE, dd MMM yyyy HH:mm zzzz",
"yyyy-MM-dd'T'HH:mm:ssZ",
"yyyy-MM-dd'T'HH:mm:ss.SSSzzzz", // Blogger Atom feed has millisecs also
"yyyy-MM-dd'T'HH:mm:sszzzz",
"yyyy-MM-dd'T'HH:mm:ss z",
"yyyy-MM-dd'T'HH:mm:ssz", // ISO_8601
"yyyy-MM-dd'T'HH:mm:ss",
"yyyy-MM-dd'T'HHmmss.SSSz",
"yyyy-MM-dd"
};
dateFormats = new SimpleDateFormat[possibleFormats.length];
TimeZone gmtTZ = TimeZone.getTimeZone("GMT");
for (int i = 0; i < possibleFormats.length; i++)
{
/* TODO: Support other locales? */
dateFormats[i] = new SimpleDateFormat(possibleFormats[i],
Locale.ENGLISH);
dateFormats[i].setTimeZone(gmtTZ);
}
}
public static Date parseDate(String str) {
Date result = null;
str = str.trim();
if (str.length() > 10) {
// TODO deal with +4:00 (no zero before hour)
if ((str.substring(str.length() - 5).indexOf("+") == 0 || str
.substring(str.length() - 5).indexOf("-") == 0)
&& str.substring(str.length() - 5).indexOf(":") == 2) {
String sign = str.substring(str.length() - 5,
str.length() - 4);
str = str.substring(0, str.length() - 5) + sign + "0"
+ str.substring(str.length() - 4);
// logger.debug("CASE1 : new date " + strdate + " ? "
// + strdate.substring(0, strdate.length() - 5));
}
String dateEnd = str.substring(str.length() - 6);
// try to deal with -05:00 or +02:00 at end of date
// replace with -0500 or +0200
if ((dateEnd.indexOf("-") == 0 || dateEnd.indexOf("+") == 0)
&& dateEnd.indexOf(":") == 3) {
// TODO deal with GMT-00:03
if ("GMT".equals(str.substring(str.length() - 9, str
.length() - 6))) {
Log.d(TAG, "General time zone with offset, no change");
} else {
// continue treatment
String oldDate = str;
String newEnd = dateEnd.substring(0, 3) + dateEnd.substring(4);
str = oldDate.substring(0, oldDate.length() - 6) + newEnd;
// logger.debug("!!modifying string ->"+strdate);
}
}
}
int i = 0;
while (i < dateFormats.length) {
try {
result = dateFormats[i].parse(str);
// logger.debug("******Parsing Success "+strdate+"->"+result+" with
// "+dateFormats[i].toPattern());
break;
} catch (java.text.ParseException eA) {
i++;
}
}
return result;
}
/**
* Format a date in a manner that would be most suitable for serialized
* storage.
*
* @param date
* {@link Date} object to format.
*
* @return
* Robust, formatted date string.
*/
public static String formatDate(Date date) {
return dateFormats[defaultFormat].format(date);
}
}
Related examples in the same category
1. | Write Date to AudioTrack | | |
2. | Represents a date using an integer, in a similar fashion to the implementation in Microsoft Excel. | | |
3. | A formatter that formats dates to show the elapsed time relative to some base date. | | |
4. | Iso Date | | |
5. | Get the string of the date using format "yyyy-MM-dd HH:mm:ss" | | |
6. | Parse a string that contains date, return a date object using format "yyyy-MM-dd HH:mm:ss" | | |
7. | Create Date from timestamp | | |
8. | Convert date in RFC2822 and UTC strings, and to build Date from string of dates in RFC2822 and UTC format | | |
9. | Gets a "HH:mm:ss.SSS EEE dd MMM yyyy" representation of a Date | | |
10. | Gets a "yyyy MMM dd, HH:mm:ss.SSS" representation of a Date | | |
11. | Gets a "EEE, dd MMM yyyy hh:mm:ss 'GMT'" representation of a Date | | |
12. | Parses a String for a "EEE, dd MMM yyyy hh:mm:ss 'GMT'" formatted Date | | |
13. | Formatting and parsing the various date formats we expect to encounter. | | |
14. | ISO8601, ISO8601, RFC822 Date format | | |
15. | Easter Date | | |
16. | Parse Date | | |
17. | compare Two dates with Day value | | |
18. | compare Two dates with Second value | | |
19. | Get start of a date | | |
20. | Get start date of a Month | | |
21. | Get start date of a year | | |
22. | Create date from year, month and day value | | |
23. | Create Date from year, month, day, hour, minute, second | | |
24. | Get year value from Date | | |
25. | Get Month value from Date | | |
26. | Get Day value from Date | | |
27. | Get now in Date and Millis-seconds | | |
28. | Add day, Month and year to a Date | | |
29. | Parse date in format of yyyyMMddHHmmss or yyyyMMdd | | |
30. | String to Date | | |
31. | Convert date value in long to YYYYMMDDHHMMSS format | | |
32. | Convert Date to Number | | |
33. | Get short and long date String | | |
34. | Convert Java Date To Xml Time | | |
35. | Convert dates to Julian dates. | | |
36. | Parse an RFC 822 date string. | | |
37. | Format a date into a format suitable for SQLite | | |
38. | Dot String to Date | | |
39. | Parses an RFC822 formatted date string. | | |
40. | Formats a Date according to RFC822. | | |
41. | DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(date); | | |
42. | yyyy-MM-dd HH:mm:ss date format | | |
43. | Utility class for formatting and parsing the various date formats we expect to encounter. | | |
44. | Get a string representation of a date as it relates to the current time. | | |
45. | get Date String from milliseconds | | |
46. | Generate a ISO 8601 date | | |
47. | Generate a Calendar from ISO 8601 date | | |
48. | date To String | | |
49. | Get date string for Locale tr | | |
50. | RFC3339 Date | | |
51. | Date formats | | |
52. | build Date Format day-of-week Short | | |
53. | Get end of each day | | |
54. | Get the end of each Month | | |
55. | Get end of a year | | |
56. | calculate Month Distance | | |
57. | calculate Day Distance | | |
58. | The month, and just the month, is zero-based. Add 1 for display. | | |
59. | Get hour different | | |
60. | format Millis Into Human Readable | | |