java JodaTime remove seconds

I am using JodaTime to create ISO 8601 String.

DateTime jodatime = new DateTime(2016, 04, 05, 23, 59, 59, 999, DateTimeZone.UTC);
String converted = jodatime.toDateTimeISO().toString();

Right now, I am getting the following:

2016-04-06T06:59:59.999Z

However, I want to truncate/remove seconds and milliseconds.

2016-04-05T23:59Z

Does anyone know how to do this with the least hacky way? And can anyone tell me if that shortened version of ISO8601 can be recognized by date parsing libraries?

Jon Skeet
people
quotationmark

The normal way of formatting a Joda Time value is using a formatter. In this case, the format you want is already available, except for the Z:

DateTimeFormatter formatter = ISODateTimeFormat.dateHourMinute();
String text = formatter.print(value);

The Z is slightly tricky - I don't believe you can specify exactly what you want with a simple pattern (DateTimeFormat.forPattern) but you can use a DateTimeFormatterBuilder:

DateTimeFormatter formatter = new DateTimeFormatterBuilder()
    .appendYear(4, 9)
    .appendLiteral('-')
    .appendMonthOfYear(2)
    .appendLiteral('-')
    .appendDayOfMonth(2)
    .appendLiteral('T')
    .appendHourOfDay(2)
    .appendLiteral(':')
    .appendMinuteOfHour(2)
    .appendTimeZoneOffset("Z", true, 2, 4)
    .toFormatter()
    .withLocale(Locale.US);

I believe that does exactly what you want.

people

See more on this question at Stackoverflow