Ujjwal
Ujjwal

Reputation: 613

Joda-Time exception due to time zone: Invalid format

Using joda-time 2.3, I am trying to parse date-time value having time zone information as well.

Below is my code -

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class App {
    public static void main(String[] args) {
        DateTime dt = new DateTime();
        DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy/MM/dd hh:mm:ss a z");
        String str = formatter.print(dt);
        System.out.println(str);

        DateTime date = formatter.parseDateTime(str);
        System.out.println(date);
    }
}

pom.xml -

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.3</version>
</dependency>

Output -

2013/09/16 11:15:03 PM IST
Exception in thread "main" java.lang.IllegalArgumentException: Invalid format: "2013/09/16 11:15:03 PM IST" is malformed at "IST"
    at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:873)
    at com.cmcltd.servermon.App.main(App.java:15)

I don't understand - the formatter pattern that use to format a DateTime object fails when I use it to parse a String into DateTime. Is this a bug in Joda-Time or intended behavior?

What format pattern can I use to parse an input date "2013/09/16 11:15:03 PM IST"?

Thanks.

Upvotes: 2

Views: 4037

Answers (1)

Simon Bungartz
Simon Bungartz

Reputation: 44

As it says in the documentation of DateTimeFormat:

Time zone names ('z') cannot be parsed.

Take a look at Why Joda DateTimeFormatter cannot parse timezone names ('z') to find out why

Upvotes: 2

Related Questions