How can i format joda DateTime in this style "yyMMddHHmmss"?

2.4k Views Asked by At

i tried doing this:

    DateTime dt=new DataTime(DatetimeZone.UTC);
    DateTimeFormatter diff=DatTimeFormat.forPattern("yyMMddHHmmss");
    DateTime jtime=diff.ParseDateTime(dt.toString());

But am getting illegal argument exception. please help me to resolve this thanks in advance.

2

There are 2 best solutions below

0
On BEST ANSWER

Try like this;

    DateTime today = new DateTime().withZone(DateTimeZone.UTC);
    System.out.println(today.toString(DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z")));

or this;

    DateTime dt = new DateTime(DateTimeZone.UTC);
    DateTimeFormatter fmt = ISODateTimeFormat.dateTimeNoMillis();
    String str = fmt.print(dt);
    System.out.println(str);

Also from JODA Javadoc; toString() method for DateTime outputs the date in ISO8601.

0
On

I tried a lot and came up with this solution

dt.toString("yyMMddHHmmss");