Android time in iso 8601

using android and joda time lib - I'm trying to convert the user's time zone to format it later: 2012-11-12T21: 45: 00 + 02: 00, for example.

DateTimeZone zone = DateTimeZone.forID( TimeZone.getDefault().getID()); 

the above code does not work - does anyone know how I can take Europe / London (Timezone.getID) and convert it to an offset so that I can put it in ISO 8601 format?

+4
source share
2 answers

If I understand your purpose correctly, you can directly use the SimpleDateFormat class.

Code example:

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

You can see the documentation in SimpleDateFormat

Sincerely.

+6
source

API Level 26 added support for many time and date classes from Java. Now this solution can also be applied: fooobar.com/questions/188560 / ...

 // 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 for JodaStephen.

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

+1
source

Source: https://habr.com/ru/post/1447757/


All Articles