首页>代码>java swing编写Quartz表达式生成器CronExpBuilder>/CronExpBuilder/src/wjw/cron/ex/DateFormatUtil.java
package wjw.cron.ex;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 使用ThreadLocal以空间换时间解决SimpleDateFormat线程安全问题。
 *
 * @author wstone
 */
public abstract class DateFormatUtil {

  private static final ThreadLocal<Map<String, DateFormat>> _threadLocal = new ThreadLocal<Map<String, DateFormat>>() {

    protected Map<String, DateFormat> initialValue() {
      return new HashMap<String, DateFormat>();
    }
  };

  public static DateFormat getDateFormat(String pattern) {
    DateFormat dateFormat = _threadLocal.get().get(pattern);
    if (dateFormat == null) {
      dateFormat = new SimpleDateFormat(pattern);
      _threadLocal.get().put(pattern, dateFormat);
    }
    return dateFormat;
  }

  public static Date parse(String pattern, String textDate) throws ParseException {
    return getDateFormat(pattern).parse(textDate);
  }

  public static String format(String pattern, Date date) {
    return getDateFormat(pattern).format(date);
  }
}
最近下载更多
1358849392  LV21 2022年11月23日
675104182  LV14 2020年9月22日
raven123  LV1 2020年1月7日
mingdongkeji  LV12 2019年12月23日
werwrer  LV6 2019年11月4日
leonwang12341  LV1 2019年9月8日
小小滑头鱼  LV26 2019年6月20日
qiheideguang  LV18 2019年6月13日
luocheng  LV18 2019年4月4日
toushita  LV9 2019年2月22日
最近浏览更多
dearxo2014  LV1 2024年11月9日
Hachi6  LV13 2022年12月4日
1358849392  LV21 2022年11月23日
Firms11 2022年8月24日
暂无贡献等级
cz8857216  LV4 2022年5月9日
denliv_hui  LV13 2021年10月20日
一个好人520  LV10 2021年9月29日
interface  LV22 2021年6月14日
无花空折枝  LV9 2021年5月8日
15817213218  LV12 2020年11月26日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友