How to convert a java.util.Date object into a limited form of the canonical representation of dateTime

I need to convert the java.util.Date variable to a view similar to the one below.

 1995-12-31T23:59:59.999Z

The format of this date field is: 1995-12-31T23: 59: 59Z and is a more limited form of the canonical representation of dateTime http://www.w3.org/TR/xmlschema-2/#dateTime .

The ending "Z" indicates UTC time and is mandatory.

+3
source share
1 answer

You can use SimpleDateFormat.

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(df.format(new Date()));

Output:

2010-02-14T06:38:48.920Z

Here you manually set the time zone for the date format in UTC.

+7
source

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


All Articles