Here you can find the source of getNowFormattedDateTimeStr(String pattern)
public static String getNowFormattedDateTimeStr(String pattern)
//package com.java2s; //License from project: Apache License import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; public class Main { private static Map<String, SimpleDateFormat> pattern2FormatMap = new HashMap<>(); public static String getNowFormattedDateTimeStr(String pattern) { return getFormattedDateTimeStr(pattern, new Date()); }//from w w w . j a va 2 s . com public static String getFormattedDateTimeStr(String pattern, Date date) { SimpleDateFormat simpleDateFormat = getDateFormatFromCache(pattern); return simpleDateFormat.format(date); } private static SimpleDateFormat getDateFormatFromCache(String pattern) { SimpleDateFormat simpleDateFormat = null; if (pattern2FormatMap.containsKey(pattern)) { simpleDateFormat = pattern2FormatMap.get(pattern); } else { simpleDateFormat = new SimpleDateFormat(pattern); pattern2FormatMap.put(pattern, simpleDateFormat); } return simpleDateFormat; } }