Here you can find the source of convertDateToXSDString(Date date)
Date
into the canonical lexical representation of an XSD dateTime with the following exceptions: - Dates before 1 CE (i.e.
Parameter | Description |
---|---|
date | Instance of java.util.Date. |
public static String convertDateToXSDString(Date date)
//package com.java2s; /* The contents of this file are subject to the license and copyright terms * detailed in the license directory at the root of the source tree (also * available online at http://fedora-commons.org/license/). *///from ww w . j a v a2 s . c om import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; public class Main { private static final Date ONE_BCE = new Date(-62167392000000L); private static final Date ONE_CE = new Date(-62135769600000L); /** * Converts an instance of <code>Date</code> into the canonical lexical * representation of an XSD dateTime with the following exceptions: - Dates * before 1 CE (i.e. 1 AD) are handled according to ISO 8601:2000 Second * Edition: "0000" is the lexical representation of 1 BCE "-0001" is the * lexical representation of 2 BCE * * @param date * Instance of java.util.Date. * @return the lexical form of the XSD dateTime value, e.g. * "2006-11-13T09:40:55.001Z". * @see <a * href="http://www.w3.org/TR/xmlschema-2/#date-canonical-representation">3.2.7.2 * Canonical representation</a> */ public static String convertDateToXSDString(Date date) { if (date == null) { return null; } StringBuilder lexicalForm; String dateTime = convertDateToString(date, true); int len = dateTime.length() - 1; if (dateTime.indexOf('.', len - 4) != -1) { while (dateTime.charAt(len - 1) == '0') { len--; // fractional seconds may not with '0'. } if (dateTime.charAt(len - 1) == '.') { len--; } lexicalForm = new StringBuilder(dateTime.substring(0, len)); lexicalForm.append('Z'); } else { lexicalForm = new StringBuilder(dateTime); } if (date.before(ONE_CE)) { DateFormat df = new SimpleDateFormat("yyyy"); df.setTimeZone(TimeZone.getTimeZone("UTC")); StringBuilder year = new StringBuilder(String.valueOf(Integer.parseInt(df.format(date)) - 1)); while (year.length() < 4) { year.insert(0, '0'); } lexicalForm.replace(0, lexicalForm.indexOf("-", 4), year.toString()); if (date.before(ONE_BCE)) { lexicalForm.insert(0, "-"); } } return lexicalForm.toString(); } /** * Converts an instance of java.util.Date into a String using the date * format: yyyy-MM-ddTHH:mm:ss.SSSZ. * * @param date * Instance of java.util.Date. * @return ISO 8601 String representation (yyyy-MM-ddTHH:mm:ss.SSSZ) of the * Date argument or null if the Date argument is null. */ public static String convertDateToString(Date date) { return convertDateToString(date, true); } /** * Converts an instance of java.util.Date into an ISO 8601 String * representation. Uses the date format yyyy-MM-ddTHH:mm:ss.SSSZ or * yyyy-MM-ddTHH:mm:ssZ, depending on whether millisecond precision is * desired. * * @param date * Instance of java.util.Date. * @param millis * Whether or not the return value should include milliseconds. * @return ISO 8601 String representation of the Date argument or null if * the Date argument is null. */ public static String convertDateToString(Date date, boolean millis) { if (date == null) { return null; } else { DateFormat df; if (millis) { df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); } else { df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); } df.setTimeZone(TimeZone.getTimeZone("UTC")); if (date.before(ONE_CE)) { StringBuilder sb = new StringBuilder(df.format(date)); sb.insert(0, "-"); return sb.toString(); } else { return df.format(date); } } } }