How to convert Java util. The date object is converted to a restricted form of the canonical representation of datetime

I need to put Java util. The date variable is converted to a representation similar to the following

1995-12-31T23:59:59.999Z

The format of this date field is 1995-12-31t23:59:59z, and it is datetime http://www.w3.org/TR/xmlschema-2/#dateTime The more restricted form of the canonical representation of

The trailing "Z" indicates UTC time and is mandatory

Solution

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 on the date formatter to UTC

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>