Java TimeZone Format String2Date(String date, String formatPattern, Locale locale, TimeZone timeZone)

Here you can find the source of String2Date(String date, String formatPattern, Locale locale, TimeZone timeZone)

Description

String Date

License

Mozilla Public License

Parameter

Parameter Description
date String
formatPattern String
locale Locale
timeZone TimeZone

Exception

Parameter Description

Return

Date

Declaration

public static Date String2Date(String date, String formatPattern, Locale locale, TimeZone timeZone)
        throws ParseException 

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.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {
    private static final ThreadLocal<HashMap<String, SimpleDateFormat>> simpleDateFormatLocal = new ThreadLocal<>();

    /**//from  w ww .j  av a 2s . c om
     *
     * @param date String
     * @param formatPattern String
     * @param locale Locale
     * @param timeZone TimeZone
     * @return Date
     * @throws java.text.ParseException
     */
    public static Date String2Date(String date, String formatPattern, Locale locale, TimeZone timeZone)
            throws ParseException {
        if ((formatPattern == null) || formatPattern.isEmpty()) {
            formatPattern = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat df = getDateFormat(formatPattern, locale, timeZone);
        return df.parse(date);
    }

    /**
     *
     * @param date
     * @param formatPattern
     * @return Date
     * @throws ParseException
     */
    public static Date String2Date(String date, String formatPattern) throws ParseException {
        return String2Date(date, formatPattern, null, null);
    }

    /**
     *
     * @param date
     * @return Date
     * @throws java.text.ParseException
     */
    public static Date String2Date(String date) throws ParseException {
        return String2Date(date, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     *
     * @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. getTimeWithFormat(String stringDate, String dateFormat, DateTimeZone timeZone, Locale locale)
  2. getTimezoneFormattedString(Date date, String timezone)
  3. getUserToServerDateTimeString(TimeZone timeZone, int dateFormat, int timeFormat, String date)
  4. getZfgcTimeZoneDateFormat(String timezone)
  5. resolveTimeZone(SimpleDateFormat sdf, String timezone)
  6. string2Timezone(String srcFormater, String srcDateTime, String dstFormater, String dstTimeZoneId)
  7. toAPITimeString(Date date, String format, String timeZone)
  8. toTimeFormat(String timeFormat, TimeZone tz, Locale locale)