Java Locale Format format(String pattern, Date date)

Here you can find the source of format(String pattern, Date date)

Description

Formats the specific date into the given pattern

License

Open Source License

Parameter

Parameter Description
pattern date pattern
date date to be formatted

Return

formated string representing the give date

Declaration

public static String format(String pattern, Date date) 

Method Source Code

//package com.java2s;
/*/*from  w w w . j a v  a2  s.c o 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 {
    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 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. format(Number number, Locale locale)
  2. format(Object input, int style)
  3. format(String bundleName, Locale locale, String key, Object arg1)
  4. format(String format, Date date, Locale locale)
  5. format(String message, Object[] params)
  6. format12(final long ticks)
  7. formatAdena(long amount)
  8. formatAdena(long amount)
  9. formatarDataArquivoStr(String data)