Here you can find the source of format(String pattern, Date date)
Parameter | Description |
---|---|
pattern | date pattern |
date | date to be formatted |
public static String format(String pattern, Date date)
//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; } }