SimpleDateFormat两个著名的坑

本文介绍了Java中SimpleDateFormat的线程安全问题,当作为静态变量使用时可能导致解析错误和异常。解决方案包括使用ThreadLocal确保每个线程有自己的实例,或者使用Java 8的DateTimeFormatter,后者不仅线程安全,还能在格式不匹配时抛出异常。文章还展示了如何使用ThreadLocal实现线程安全的日期格式化。

坑1:定义的static的SimpleDateFormat 可能出现线程安全问题

SimpleDateFormat是线程不安全的类,定义为static对象,会有数据同步风险。通过源码可以看出,SimpleDateFormat内部有一个Calendar对象,在日期转字符串或字符串转日期的过程中,多线程共享时有非常高的概率产生错误,推荐的方式之一时使用ThreadLocal,让每个线程单独拥有这个对象。

示例代码:

public class SimpleDateFormatterTest {
    static ExecutorService threadPool = Executors.newFixedThreadPool(20);

    static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static void main(String[] args) {
        for(int i=0;i<200;i++){
            threadPool.execute(()->{
                try {
                    System.out.println(format.parse("2021-07-09 16:29:21"));
                } catch (ParseException e) {
                    e.printStackTrace();
                };
            });
        }
    }
}

运行程序后大量报错,且没有报错的输出结果也不正确

在这里插入图片描述
在这里插入图片描述
为什么会出现上述问题呢?
SimpleDateFormat 的作用是的另一解析和格式化日期时间的模式,这看起来是一次性工作。应该复用,但它的解析和格式化的操作是非线程安全的。

*SimpleDateFormat 继承 DateFormat ,DateFormat 有一个成员变量 calendar。

SimpleDateFormat 的parse 方法如下:

    public Date parse(String source) throws ParseException
    {
        ParsePosition pos = new ParsePosition(0);
        Date result = parse(source, pos);
        if (pos.index == 0)
            throw new ParseException("Unparseable date: \"" + source + "\"" ,
                pos.errorIndex);
        return result;
    }
 

最终会调用CalendarBuilder的establish 方法来构建Calendar

parsedDate = calb.establish(calendar).getTime();

establish 方法内部是先清空 Calendar再构建Calendar,整个的操作没有加锁

 Calendar establish(Calendar cal) {
    ......

        cal.clear();
   
    ......
        return cal;
    }
    ```
 如果多线程在并发操作一个Calendar, 可能会产生一个线程还没来得及处理Calendar 就被另外一个线程清空了,所以会出现解析错误和异常。

那么怎么解决呢?

* 每次使用时new一个SimpleDateFormat 的 实例,这样可以保证每个实例使用自己的Calendar实例,但是每次使用都需要new一个对象,并且使用后由于没有其他引用,又需要回收,开销会很大。
* 可以使用syncheronized 对SimpleDtaFormat实例进行同步
* 【推荐】 使用ThreadLocl,这样每个线程只需要使用一个SimpleDateFormate实例,这相比第一种方式 节省了对象的创建销毁开销,并且不需要使多个线程同步。
* 使用Java8的DateTimeFormatter 类

下面是用ThreadLocal实现的示例:
```java
public class SimpleDateFormatterTest {
    static ExecutorService threadPool = Executors.newFixedThreadPool(20);
    private static  final ThreadLocal<SimpleDateFormat> SIMPLEDATEFORMAT_THREADLOCAL = new ThreadLocal<SimpleDateFormat>(){
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };

    static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static void main(String[] args) throws InterruptedException {
        for(int i=0;i<200;i++){
            threadPool.execute(()->{
                try {
                    System.out.println(SIMPLEDATEFORMAT_THREADLOCAL.get().parse("2021-07-09 16:29:21"));
                } catch (ParseException e) {
                    e.printStackTrace();
                };
            });
        }
        threadPool.shutdown();
        threadPool.awaitTermination(10, TimeUnit.SECONDS);
    }
}

坑2:当需要解析的字符串和格式不匹配的时候,SimpleDateFormat 并不报错,而是返回其他日期

    public static void main(String[] args) throws InterruptedException, ParseException {
       String dateString = "20210908";
       SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMM");
       System.out.println(simpleDateFormat.parse(dateString));
    }

结果:
Wed Aug 01 00:00:00 CST 2096

竟然输出了 2096年了

对于上面的两个坑,我们可以使用java8的DateTimeFormatter 来避免

1) 解决第一个坑

public class DateTimeFormatterTest {
    static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    static ExecutorService threadPool = Executors.newFixedThreadPool(20);

    public static void main(String[] args) throws InterruptedException {
        for(int i=0;i<200;i++){
            threadPool.execute(()->{
                  System.out.println(dateTimeFormatter.parse("2021-07-09 16:29:21"));
            });
        }
        threadPool.shutdown();
        threadPool.awaitTermination(10, TimeUnit.SECONDS);
    }

}
  1. 解决第二个坑
 DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMM");
        System.out.println(dateTimeFormatter.parse("2021-07-09 16:29:21"));

此时会直接报错,而不会出现不正确的结果;

DateTimeFormatter 是线程安全的,可以定义为static 使用,最后,DateTimeFormatter 的解析比较严格,需要解析的字符串和格式不匹配时,会直接报错。而不是错误的解析。

### BeanUtils.copyProperties 常见问题及解决方法 #### 属性访问器缺失引发异常 当目标类和源类的属性未提供 `getter` 和 `setter` 方法时,调用 `BeanUtils.copyProperties()` 将会抛出 `NoSuchMethodException` 异常[^2]。为了防止这种情况发生,建议确保所有需要被复制的字段都已定义相应的存取方法。 #### 性能考量 鉴于 `BeanUtils.copyProperties()` 利用了反射技术来进行操作,在面对大规模的数据迁移任务时可能构成性能障碍[^4]。针对高频率或大数据量的应用环境,推荐评估并选用更为高效的替代方案,比如手写赋值逻辑或是借助第三方库(例如 MapStruct 或 Dozer),它们通常能够带来更好的执行效率。 #### 类型匹配处理 有时两个实体间存在同名但同类型的成员变量;此时即使提供了完整的 getter/setter 接口也可能无法顺利完成映射过程。遇到此类情况应提前校验待转换对象结构的一致性,并通过自定义编辑器或者类型处理器来适配差异化的数据格式。 ```java // 自定义 PropertyEditor 支持特定类型转换 public class CustomDateEditor extends PropertyEditorSupport { private final SimpleDateFormat format; public CustomDateEditor(String pattern) { this.format = new SimpleDateFormat(pattern); } @Override public void setAsText(String text) throws IllegalArgumentException { try { setValue(format.parse(text)); } catch (ParseException e) { throw new IllegalArgumentException(e.getMessage(), e); } } } ``` #### 集合与复杂嵌套对象的支持 对于包含集合或其他复合类型作为其组成部分的目标/源头实例来说,默认行为下的 `copyProperties` 只做浅层拷贝而深入内部节点进行深克隆。这可能导致意外的结果特别是涉及到可变状态共享的情形下。因此有必要根据实际需求调整策略以实现深层次的对象图遍历与重建工作。 ```java // 手动处理复杂的嵌套对象 public static <T> T deepCopy(Object source, Class<T> targetClass){ if(source == null || !source.getClass().isAnnotationPresent(ComplexObject.class)){ return org.springframework.beans.BeanUtils.copyProperties(source,targetClass.newInstance()); }else{ // 实现深度复制逻辑... } } ```
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半夏_2021

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值