Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joda DateTime ISODateTimeFormat pattern

The Joda ISODateTimeFormat docs say ISODateTimeFormat.dateTime() returns a formatter for the pattern yyyy-MM-dd'T'HH:mm:ss.SSSZZ

But the formatter returns a "Z" in place of +00:00
see this-

DateTime dt = DateTime.now(DateTimeZone.UTC);

DateTimeFormatter patternFormat = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZZ");
DateTimeFormatter isoFormat = ISODateTimeFormat.dateTime();

System.out.println(dt.toString(patternFormat));     //2014-06-01T03:02:13.552+00:00
System.out.println(dt.toString(isoFormat));         //2014-06-01T03:02:13.552Z

Can anyone tell me what the pattern would be to get the +00:00 to print as a Z

Edit: Just to clarify- I know that the 'Z' is the same as +00:00 but textually it is different. What I am asking is what pattern would place a Z as the time offset instead of +00:00

(Sorry if this is too trivial. I wanted to use the ISO format without milliseconds and in the course of writing this question I found exactly what I was after in ISODateTimeFormat.dateTimeNoMillis() so I am asking now only for interests sake)

like image 444
Dave Pile Avatar asked Jun 02 '14 04:06

Dave Pile


People also ask

What is isodatetimeformat public class?

Class ISODateTimeFormat. public class ISODateTimeFormat extends Object Factory that creates instances of DateTimeFormatter based on the ISO8601 standard. Date-time formatting is performed by the DateTimeFormatter class. Three classes provide factory methods to create formatters, and this is one.

How to use pattern in datetime format in Java?

For example, to use a pattern: DateTime dt = new DateTime(); DateTimeFormatter fmt = DateTimeFormat.forPattern("MMMM, yyyy"); String str = fmt.print(dt); The pattern syntax is mostly compatible with java.text.SimpleDateFormat - time zone names cannot be parsed and a few more symbols are supported.

What is a pattern-based datetimeformat?

The class DateTimeFormat provides a single method forPattern (String) that supports formatting by pattern. These "pattern-based" formatters provide a similar approach to that of SimpleDateFormat. For example:

What is style-based formatters in Joda-Time?

Joda-Time provides a comprehensive formatting system. There are two layers: High level - pre-packaged constant formatters; ... These "style-based" formatters handle the case where the application needs to format a date-time in a manner appropriate to a particular global location. Pattern-based formatting.


1 Answers

It doesn't appear that you can build such a formatter purely from a pattern. The DateTimeFormat doc says:

Zone:

  • 'Z' outputs offset without a colon,
  • 'ZZ' outputs the offset with a colon,
  • 'ZZZ' or more outputs the zone id.

You can build most of the formatter from a pattern and then customise the time zone output like this:

    DateTimeFormatter patternFormat = new DateTimeFormatterBuilder()
        .appendPattern("yyyy-MM-dd'T'HH:mm:ss.SSS")
        .appendTimeZoneOffset("Z", true, 2, 4)
        .toFormatter();
like image 75
araqnid Avatar answered Nov 15 '22 13:11

araqnid