Here you can find the source of Date2String(Date date, String formatPattern)
Parameter | Description |
---|---|
date | a parameter |
formatPattern | a parameter |
public static String Date2String(Date date, String formatPattern)
//package com.java2s; /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ import java.text.SimpleDateFormat; import java.util.*; public class Main { private static final ThreadLocal<HashMap<String, SimpleDateFormat>> simpleDateFormatLocal = new ThreadLocal<>(); /**//from ww w .j a va 2 s . c o m * * @param date Date * @return String */ public static String Date2String(Date date) { return Date2String(date, "yyyy-MM-dd HH:mm:ss", null, null); } /** * * @param date * @param formatPattern * @return String */ public static String Date2String(Date date, String formatPattern) { return Date2String(date, formatPattern, null, null); } /** * * @param date * @param formatPattern * @param locale * @param timeZone * @return String */ public static String Date2String(Date date, String formatPattern, Locale locale, TimeZone timeZone) { if ((formatPattern == null) || formatPattern.isEmpty()) { formatPattern = "yyyy-MM-dd HH:mm:ss"; } SimpleDateFormat df = getDateFormat(formatPattern, locale, timeZone); return df.format(date); } /** * * @param formatPattern * @param locale Locale * @param timeZone TimeZone * @return SimpleDateFormat */ private static SimpleDateFormat getDateFormat(String formatPattern, Locale locale, TimeZone timeZone) { HashMap<String, SimpleDateFormat> dfmap = simpleDateFormatLocal.get(); String key = locale == null ? formatPattern : (formatPattern + locale.toString()); SimpleDateFormat df; if (dfmap == null) { dfmap = new HashMap<>(); df = locale == null ? new SimpleDateFormat(formatPattern) : new SimpleDateFormat(formatPattern, locale); dfmap.put(key, df); simpleDateFormatLocal.set(dfmap); } else { df = dfmap.get(key); if (df == null) { df = locale == null ? new SimpleDateFormat(formatPattern) : new SimpleDateFormat(formatPattern, locale); if (dfmap.size() < 100) { dfmap.put(key, df); } } } if (timeZone == null) { df.setTimeZone(TimeZone.getDefault()); } else { df.setTimeZone(timeZone); } return df; } }