Joda时间,解析不起作用

时间:2016-11-18 09:16:00

标签: java jodatime

我有这段代码,不知道为什么它不起作用

String dateTime = "Sun, 18 Apr 2004 02:32:43";
// Format for input
org.joda.time.format.DateTimeFormatter dtf = DateTimeFormat.forPattern("EEE, dd MMM yyyy kk:mm:ss");
// Parsing the date
DateTime jodatime = dtf.parseDateTime(dateTime);
// Format for output
org.joda.time.format.DateTimeFormatter dtfOut = DateTimeFormat.forPattern("dd MMM yyyy kk:mm:ss");
// Printing the date

错误我得到了:

Exception in thread "main" java.lang.IllegalArgumentException: Invalid format: "Sun, 18 Apr 2004 02:32:43"
    at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:945)
    at com.jbd.WeirdCutEmails.test(WeirdCutEmails.java:69)
    at com.jbd.WeirdCutEmails.main(WeirdCutEmails.java:108)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

3 个答案:

答案 0 :(得分:4)

(感谢汤姆的建议)

这很可能是Locale问题:如果您的默认Locale无法识别'Sun'和'Apr',则DateTimeFormatter会抛出IllegalArgumentException

您可以使用withLocale(Locale locale)

解决此问题
DateTimeFormat.forPattern("EEE, dd MMM yyyy kk:mm:ss")
    .withLocale(Locale.ENGLISH)

插图:

@Before
public void setUp() throws Exception {
    Locale.setDefault(new Locale("pt", "BR"));
}

@Test(expected = IllegalArgumentException.class)
public void testDefaultFormatterWontParseDifferentLocale() {
    //arrange
    DateTimeFormatter dtf = DateTimeFormat.forPattern("EEE, dd MMM yyyy kk:mm:ss");

    //act
    dtf.parseDateTime("Sun, 18 Apr 2004 02:32:43"); //won't parse as expecting a String in Portuguese Locale
}

@Test
public void testFormatterWithSuppliedLocale() {
    //arrange
    DateTimeFormatter dtf = DateTimeFormat
                                .forPattern("EEE, dd MMM yyyy kk:mm:ss")
                                .withLocale(Locale.ENGLISH);

    //act
    DateTime actualDateTime = dtf.parseDateTime("Sun, 18 Apr 2004 02:32:43");

    //assert
    Assert.assertEquals(new DateTime(2004,4,18,2,32,43), actualDateTime);
}

@After
public void tearDown() throws Exception {
    Locale.setDefault(new Locale("en","US"));
}

答案 1 :(得分:3)

这是有效的。试试这个。

String dateTime = "Sun, 18 Apr 2004 02:32:43";
// Format for input
DateTimeFormatter dtf = DateTimeFormat.forPattern("EEE, dd MMM yyyy kk:mm:ss");

// Parsing the date
System.out.println(dtf.parseDateTime(dateTime));

// Format for output
DateTimeFormatter dtfOut = DateTimeFormat.forPattern("EEE, dd MMM yyyy kk:mm:ss");

// Printing the date
System.out.println(dtfOut.parseDateTime(dateTime));

答案 2 :(得分:1)

org.joda.time.format.DateTimeFormatter dtf = DateTimeFormat.forPattern("EEE, dd MMM yyyy kk:mm:ss");

的未成年人

使用org.joda.time.format.DateTimeFormatter dtf = DateTimeFormat.forPattern("EEE, dd MMM yyyy hh:mm:ss");

kk - > HH