Java Date Format convertDateToString(Date date)

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

Description

Converts an instance of java.util.Date into a String using the date format: yyyy-MM-ddTHH:mm:ss.SSSZ.

License

fedora commons license

Parameter

Parameter Description
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.

Declaration

public static String convertDateToString(Date date) 

Method Source Code


//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/).
 *//*  www  . j av a 2s. c  o  m*/

import java.text.DateFormat;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    private static final Date ONE_CE = new Date(-62135769600000L);

    /**
     * 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);
            }
        }
    }
}

Related

  1. convertDateToString(Date aDate)
  2. convertDateToString(Date date)
  3. convertDateToString(Date date)
  4. convertDateToString(Date date)
  5. convertDateToString(Date date)
  6. convertDateToString(Date date)
  7. convertDateToString(Date date, boolean millis)
  8. convertDateToString(Date date, int addHours)
  9. convertDateToString(Date date, String dateFormat)