Monday, June 11, 2018

java - How to get current moment in ISO 8601 format with date, hour, and minute?



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



Example:



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

Answer



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.setTimeZone(tz);
String nowAsISO = df.format(new Date());


Using a new Date() as shown above will format the current time.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...