Here you can find the source of format(Calendar calendar)
public static String format(Calendar calendar)
//package com.java2s; //License from project: Apache License import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.Map; public class Main { private static final String a = "yyyy-MM-dd HH:mm:ss:SSS"; private static final String e = "yyyy-MM-dd HH:mm:ss"; private static final ThreadLocal<Map<String, SimpleDateFormat>> threadLocal = new ThreadLocal<Map<String, SimpleDateFormat>>(); public static String format() { return getDateFormat(a).format(new Date()); }//from w w w . j av a 2 s .c o m public static String format(String s) { return getDateFormat(s).format(new Date()); } public static String format(Calendar calendar) { return getDateFormat(a).format(calendar.getTime()); } public static String format(Calendar calendar, String s) { return getDateFormat(s).format(calendar.getTime()); } public static String format(Date date) { return getDateFormat(e).format(date); } public static String format(Date date, String s) { return getDateFormat(s).format(date); } private static SimpleDateFormat getDateFormat(String format) { Map<String, SimpleDateFormat> map = threadLocal.get(); if (map == null) { map = new HashMap<String, SimpleDateFormat>(); threadLocal.set(map); } SimpleDateFormat f = map.get(format); if (f == null) { f = new SimpleDateFormat(format); map.put(format, f); } return f; } }