Android time in iso 8601

If I have correctly understood your objective you can use directly the SimpleDateFormat class.

Example code:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.UK);
String formattedDate = sdf.format(new Date());

You can see documentation in SimpleDateFormat

Regards.


API level 26 added support for many time and date classes from Java. So this solution can now also be applied: https://stackoverflow.com/a/25618897/3316651

// works with Instant
Instant instant = Instant.now();
System.out.println(instant.format(DateTimeFormatter.ISO_INSTANT));

// works with ZonedDateTime 
ZonedDateTime zdt = ZonedDateTime.now();
System.out.println(zdt.format(DateTimeFormatter.ISO_INSTANT));

// example output
2014-09-02T08:05:23.653Z

Credits to JodaStephen.

Android doc: https://developer.android.com/reference/java/time/format/DateTimeFormatter.html#ISO_INSTANT