Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (4k points)

What is the most elegant way to get ISO 8601 formatted presentation of the current moment, UTC? It should look like: 2010-10-12T08:50Z.

Example:

String iso8601 = DateFormat.getDateTimeInstance(DateFormat.ISO_8601).format(date);

2 Answers

0 votes
by (46k points)

Try SimpleDateFormat to format any Date object you require:

TimeZone tz = TimeZone.getTimeZone("UTC");

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); // Quoted "Z" to indicate UTC, no timezone offset

df.setTimeZone(tz);

String nowAsISO = df.format(new Date());

Applying a new Date() as revealed above will format the current time.

0 votes
by (140 points)
 Use SimpleDateFormat to format any Date object you want: TimeZone tz = TimeZone. getTimeZone("UTC"); DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); // Quoted "Z" to indicate UTC, no timezone offset df.


Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...