I am trying to format a date with Joda time library but my problem is that it behaves different depending in which device it is executed. This is my code:
public static String parseDate (String date)
{
final String datePattern = "yyyy-MM-dd, ";
final String hourPattern = "HH:mm";
DateTimeParser[] parsers = {
DateTimeFormat.forPattern("EEE, dd MMM yyyy HH:mm:ss Z").getParser(),
DateTimeFormat.forPattern("EEE, dd MMM yyyy HH:mm:ss ZZZ").getParser(),
DateTimeFormat.forPattern("yyy-MM-dd'T'HH:mm:ss.SSSZ").getParser()};
DateTimeFormatter formatter = new DateTimeFormatterBuilder().append( null, parsers).toFormatter();
String daux = "";
try
{
DateTime dt = formatter.parseDateTime(date);
String pubDate = dt.toString(datePattern);
String pubHour = dt.toString(hourPattern);
daux = pubDate + "kl " + pubHour;
}
catch (Exception e)
{
e.printStackTrace();
}
return daux;
}
The date “Tue, 24 Apr 2012 11:06:19 GMT” it is formatted correctly in a nexus 4 but it raises an exception when executed in a Samsung GS2. I dont know really what it could be.
The exception is:
01-22 15:21:05.160: W/System.err(11718): java.lang.IllegalArgumentException: Invalid format: "Sun, 20 Jan 2013 23:27:00 GMT"
01-22 15:21:05.160: W/System.err(11718): at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:866)
01-22 15:21:05.160: W/System.err(11718): at com.fotbollskanalen.parser.DateParser.parseDate(DateParser.java:150)
Any ideas?
You’re likely facing locale specific issues in parsing.
From DateTimeFormatter.forPattern() javadoc:
UPD
You should change the line to: