Java Date to String Date2String(Date date, String formatPattern)

Here you can find the source of Date2String(Date date, String formatPattern)

Description

Date String

License

Mozilla Public License

Parameter

Parameter Description
date a parameter
formatPattern a parameter

Return

String

Declaration

public static String Date2String(Date date, String formatPattern) 

Method Source Code

//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;
    }
}

Related

  1. date2String(Date date, int _iType)
  2. Date2String(Date date, String DateFormat)
  3. date2string(Date date, String format)
  4. date2String(Date date, String format)
  5. date2String(Date date, String format)
  6. date2String(Date date, String pattern)
  7. date2String(Date source, String formatString)
  8. date2String(Date value)
  9. date2String(java.util.Date date, String format)