2

I am trying to use Joda Time both for formatting DateTime objects to String and than parse these strings back to DateTime. But I am failing to so when the pattern includes z:

    DateTimeFormatter dtf = DateTimeFormat.forPattern("dd-MM-yyyy HH:mm:ss.SSS z");

    String dts = dtf.print(System.currentTimeMillis());

    System.out.println(dts);

    DateTime dt = dtf.parseDateTime(dts);

The above code is throwing exception when the parsing the String to DateTime takes occurred.

Do you have any idea?

Yosi

yosi
  • 639
  • 1
  • 12
  • 21

2 Answers2

1

You can do:

DateTime dt = new DateTime();
System.out.println(dt.toString("dd-MM-yyyy HH:mm:ss.SSS z"));

Have a look in the user guide

dimitrisli
  • 20,895
  • 12
  • 59
  • 63
0

The Pattern is not correct, maybe try this one

DateTimeFormatter dtf = DateTimeFormat.forPattern( "dd-MM-yyyy HH:mm:ss.SSS'z" );

this worked for me

mana
  • 6,347
  • 6
  • 50
  • 70