JDK8以前使用SImpleDateFormate类格式化日期,因为在SImple DateFormate中存在Calendar实例引用,而在caleander中得establish中存在clear()和set()操作,在多线程情况下会产生问题,导致后面得后面线程修改之前线程,因此SImpleDateFormate是线程不安全得;

JDK8中使用DateTimeFormate日期格式化。在JDK8中新增得类是final关键子修饰不可变线程安全,所以可以安全的操作日期转换。

解决SImpleDateFormate线程不安全:

    采用局部变量的方式,缺点是每次调用方法时,会有对象产生,对象的回收

    采用synchronized或lock,缺点是同一时间内只有一个线程在操作,降低效率

    采用第三方框架:joda-time

    使用ThreadLocal采用线程副本的方式,使每个线程自身拥有SimpleDateFormate对象。

public class DateUtil {

    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static ThreadLocal threadLocal = new ThreadLocal();

    public static Date parse(String textDate) throws ParseException {
        return getDateFormat().parse(textDate);
    }
    public static String format(Date date) throws ParseException {
        return getDateFormat().format(date);
    }
    public static DateFormat getDateFormat() {
        DateFormat df = (DateFormat) threadLocal.get();
        if (df == null) {
            df = new SimpleDateFormat(DATE_FORMAT);
            threadLocal.set(df);
        }
        return df;
    }

    public static void main(String[] args) throws ParseException {
        String format = DateUtil.format(new Date());
        System.out.println(format);
        Date parse = DateUtil.parse("2019-12-03 16:02:59");
        System.out.println(parse);
    }
} 
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-06
  • 2021-05-25
  • 2022-12-23
  • 2021-11-14
  • 2021-04-13
猜你喜欢
  • 2021-05-13
  • 2022-12-23
  • 2019-09-16
  • 2021-09-15
  • 2021-09-22
  • 2022-12-23
  • 2021-06-21
相关资源
相似解决方案