(credit to Tom for the offer)
This is most likely a problem Locale: if your default Localedoes not recognize "Sun" and "Apr", then it DateTimeFormatterwill call IllegalArgumentException.
You can get around this using withLocale(Locale locale):
DateTimeFormat.forPattern("EEE, dd MMM yyyy kk:mm:ss")
.withLocale(Locale.ENGLISH)
Illustration:
@Before
public void setUp() throws Exception {
Locale.setDefault(new Locale("pt", "BR"));
}
@Test(expected = IllegalArgumentException.class)
public void testDefaultFormatterWontParseDifferentLocale() {
DateTimeFormatter dtf = DateTimeFormat.forPattern("EEE, dd MMM yyyy kk:mm:ss");
dtf.parseDateTime("Sun, 18 Apr 2004 02:32:43");
}
@Test
public void testFormatterWithSuppliedLocale() {
DateTimeFormatter dtf = DateTimeFormat
.forPattern("EEE, dd MMM yyyy kk:mm:ss")
.withLocale(Locale.ENGLISH);
DateTime actualDateTime = dtf.parseDateTime("Sun, 18 Apr 2004 02:32:43");
Assert.assertEquals(new DateTime(2004,4,18,2,32,43), actualDateTime);
}
@After
public void tearDown() throws Exception {
Locale.setDefault(new Locale("en","US"));
}
source
share