Java Date to Time toXSDDateTimeString(Date d)

Here you can find the source of toXSDDateTimeString(Date d)

Description

Formats a Date into XSD dateTime format

License

Educational Community License

Parameter

Parameter Description
d the date value to format

Return

date value formatted into XSD dateTime format

Declaration

public static String toXSDDateTimeString(Date d) 

Method Source Code

//package com.java2s;
/**// ww w .  j a va  2s  .c om
 * Copyright 2005-2015 The Kuali Foundation
 *
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.opensource.org/licenses/ecl2.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.TimeZone;

public class Main {
    private static final String ZULU_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
    private static final TimeZone ZULU_TZ = TimeZone.getTimeZone("UTC");

    /**
     * Formats a Date into XSD dateTime format
     * @param d the date value to format
     * @return date value formatted into XSD dateTime format
     */
    public static String toXSDDateTimeString(Date d) {
        return createZulu().format(d);
    }

    /** date formats are not thread safe so creating a new one each time it is needed. */
    private static DateFormat createZulu() {
        final DateFormat df = new SimpleDateFormat(ZULU_FORMAT);
        df.setTimeZone(ZULU_TZ);
        return df;
    }
}

Related

  1. toTimeString(Date date)
  2. toTimeString(Date dateTime)
  3. toTimeString(Date time)
  4. toUIDateTimeString(Date d)
  5. toXSDDateTime(Date date)