Java Date Format ISO formatISO8601Date(Date date)

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

Description

Formats the specified date as an ISO 8601 string.

License

Open Source License

Parameter

Parameter Description
date The date to format.

Return

The ISO 8601 string representing the specified date.

Declaration

public static String formatISO8601Date(Date date) 

Method Source Code

//package com.java2s;
/*//from w w  w.  j ava 2s  .co  m
 * Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Portions copyright 2006-2009 James Murty. Please see LICENSE.txt
 * for applicable license terms and NOTICE.txt for applicable notices.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file 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.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

public class Main {
    /**
     * ISO 8601 format
     */
    public static final String ISO8601_DATE_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
    private static final TimeZone GMT_TIMEZONE = TimeZone.getTimeZone("GMT");
    /**
     * A map to cache date pattern string to SimpleDateFormat object
     */
    private static final Map<String, ThreadLocal<SimpleDateFormat>> sdfMap = new HashMap<String, ThreadLocal<SimpleDateFormat>>();

    /**
     * Formats the specified date as an ISO 8601 string.
     *
     * @param date The date to format.
     * @return The ISO 8601 string representing the specified date.
     */
    public static String formatISO8601Date(Date date) {
        return format(ISO8601_DATE_PATTERN, date);
    }

    /**
     * Formats the specific date into the given pattern
     *
     * @param pattern date pattern
     * @param date date to be formatted
     * @return formated string representing the give date
     */
    public static String format(String pattern, Date date) {
        return getSimpleDateFormat(pattern).get().format(date);
    }

    /**
     * A helper function to retrieve a SimpleDateFormat object for the given
     * date pattern
     *
     * @param pattern date pattern
     * @return SimpleDateFormat object
     */
    private static ThreadLocal<SimpleDateFormat> getSimpleDateFormat(final String pattern) {
        ThreadLocal<SimpleDateFormat> sdf = sdfMap.get(pattern);
        if (sdf == null) {
            synchronized (sdfMap) {
                sdf = sdfMap.get(pattern);
                if (sdf == null) {
                    sdf = new ThreadLocal<SimpleDateFormat>() {
                        @Override
                        protected SimpleDateFormat initialValue() {
                            SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.US);
                            sdf.setTimeZone(GMT_TIMEZONE);
                            sdf.setLenient(false);
                            return sdf;
                        }
                    };
                    sdfMap.put(pattern, sdf);
                }
            }
        }
        return sdf;
    }
}

Related

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