views:

33

answers:

1

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

 1995-12-31T23:59:59.999Z

The format for this date field is of the form 1995-12-31T23:59:59Z, and is a more restricted form of the canonical representation of dateTime http://www.w3.org/TR/xmlschema-2/#dateTime
The trailing "Z" designates UTC time and is mandatory.

+3  A: 

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're manually setting the time zone on the date formatter to UTC.

cletus
I was using this particularly for Solr. For this, Solr has already provided a DateField class inside the core library. org.apache.solr.schema.DateField. This can also be used for that purpose.
Ritesh M Nayak