Here you can find the source of String2Date(String date, String formatPattern, Locale locale, TimeZone timeZone)
Parameter | Description |
---|---|
date | String |
formatPattern | String |
locale | Locale |
timeZone | TimeZone |
Parameter | Description |
---|
public static Date String2Date(String date, String formatPattern, Locale locale, TimeZone timeZone) throws ParseException
//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; } }