Java Date to String dateToW3CDTF(Date date)

Here you can find the source of dateToW3CDTF(Date date)

Description

Creates a String that represents the Date passed to it in W3C Date and Time Format.

License

Open Source License

Parameter

Parameter Description
date Date to convert to W3C Date and Time Format

Return

String that represents the Date passed to it in W3C Date and Time Format

Declaration

public static String dateToW3CDTF(Date date) 

Method Source Code

//package com.java2s;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    private static final ThreadLocal<SimpleDateFormat> w3cdtf = new ThreadLocal<SimpleDateFormat>() {
        protected SimpleDateFormat initialValue() {
            try {
                return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
            } catch (Exception e) {
                throw new Error("Exception in static initializer: " + e.toString());
            }/*w  w  w.j  av a  2  s  . c  o m*/
        }
    };

    /**
     * Creates a String that represents the Date passed to it in W3C Date and
     * Time Format.
     * 
     * @param date
     *            Date to convert to W3C Date and Time Format
     * @return String that represents the Date passed to it in W3C Date and Time
     *         Format
     */
    public static String dateToW3CDTF(Date date) {
        String s = w3cdtf.get().format(date);
        int index = s.length() - 2;
        return s.substring(0, index) + ":" + s.substring(index);
    }
}

Related

  1. dateToTimeString(Date date)
  2. dateToUTCDate(Date d)
  3. dateToUtcString(Date date)
  4. dateToW3c(Date d)
  5. dateToW3CDTF(Date date)
  6. dateToyyyyMMddHHmmss(Date date)