Here you can find the source of formatDate(long time, String format)
public static String formatDate(long time, String format)
//package com.java2s; // Licensed under the Apache License, Version 2.0 (the "License"); import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Map; import java.util.TimeZone; import java.util.concurrent.ConcurrentHashMap; public class Main { private static String localZone = TimeZone.getDefault().getID(); private static Map<String, Map<String, ThreadLocal<SimpleDateFormat>>> tzDateFormatters = new ConcurrentHashMap<String, Map<String, ThreadLocal<SimpleDateFormat>>>(); /**/*from w ww . ja va2 s.c o m*/ * Formats a date using a <code>SimpleDateFormat</code> instance with the * current locale's timezone. */ public static String formatDate(Date date, String format) { return formatDate(date, format, localZone); } /** * Formats a date using a <code>SimpleDateFormat</code> instance with the * specified timezone. */ public static String formatDate(Date date, String format, String zoneId) { return getDateFormatter(format, zoneId).format(date); } /** * Formats the date held in a <code>Calendar</code> using the current * locale's timezone (<em>not</em> the calendar's own timezone). */ public static String formatDate(Calendar cal, String format) { return formatDate(cal.getTime(), format, localZone); } /** * Formats the date held in a <code>Calendar</code> using the specified * timezone (<em>not</em> the calendar's own timezone). */ public static String formatDate(Calendar cal, String format, String zoneId) { return getDateFormatter(format, zoneId).format(cal.getTime()); } /** * Formats a Java timestamp (millis since epoch) using the current * locale's timezone. */ public static String formatDate(long time, String format) { return formatDate(new Date(time), format, localZone); } /** * Formats a Java timestamp (millis since epoch) using the specified * timezone (<em>not</em> the calendar's own timezone). */ public static String formatDate(long time, String format, String zoneId) { return getDateFormatter(format, zoneId).format(new Date(time)); } private static SimpleDateFormat getDateFormatter(final String format, final String zoneId) { Map<String, ThreadLocal<SimpleDateFormat>> zoneFormatters = tzDateFormatters.get(zoneId); if (zoneFormatters == null) { zoneFormatters = new ConcurrentHashMap<String, ThreadLocal<SimpleDateFormat>>(); tzDateFormatters.put(zoneId, zoneFormatters); } ThreadLocal<SimpleDateFormat> threadLocal = zoneFormatters.get(format); if (threadLocal == null) { threadLocal = new ThreadLocal<SimpleDateFormat>() { @Override protected SimpleDateFormat initialValue() { SimpleDateFormat formatter = new SimpleDateFormat(format); formatter.setTimeZone(TimeZone.getTimeZone(zoneId)); return formatter; } }; zoneFormatters.put(format, threadLocal); } return threadLocal.get(); } }