Java Date Format ISO formatISO8601Date(final Calendar date)

Here you can find the source of formatISO8601Date(final Calendar date)

Description

Obtain an ISO 8601:2004 string representation of the date given the supplied milliseconds since the epoch.

License

Open Source License

Parameter

Parameter Description
date the date in calendar form

Return

the string in the standard format

Declaration

public static String formatISO8601Date(final Calendar date) 

Method Source Code

//package com.java2s;
/*/*from  w w w.j av  a  2  s.com*/
 * JBoss, Home of Professional Open Source.
 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors. 
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Main {
    /**
     * 
     */
    public static final String ISO_8601_2004_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
    /**
     * 
     */
    public static final String ISO_8601_2004_FORMAT_UTC = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";

    /**
     * Obtain an ISO 8601:2004 string representation of the date given the supplied milliseconds since the epoch.
     * 
     * @param date
     *            the date in calendar form
     * @return the string in the {@link #ISO_8601_2004_FORMAT standard format}
     */
    public static String formatISO8601Date(final Calendar date) {
        if (date.getTimeZone().getRawOffset() == 0) {
            return new SimpleDateFormat(ISO_8601_2004_FORMAT_UTC).format(date.getTime());
        }
        return formatISO8601Date(date.getTime());
    }

    /**
     * Obtain an ISO 8601:2004 string representation of the supplied date.
     * 
     * @param date
     *            the date
     * @return the string in the {@link #ISO_8601_2004_FORMAT standard format}
     */
    public static String formatISO8601Date(final java.util.Date date) {
        if (date == null) {
            return "";
        }
        return new SimpleDateFormat(ISO_8601_2004_FORMAT).format(date);
    }
}

Related

  1. formatISO8601Date(Date d)
  2. formatISO8601Date(Date date)
  3. formatISO8601Date(Date date)
  4. formatIso8601Date(Date date)
  5. formatISO8601Date(Date date)
  6. formatISO8601Date(final java.util.Date date)
  7. formatISO8601DateWOTime(final java.util.Date date)
  8. formatIso8601ForCCTray(Date date)
  9. formatIsoDate(Date date)