说明: 项目中出现了一个BUG, 本来日期是1651766400(2022-05-06), 但是使用simpleDateFormat转换之后变成了2022-05-05的日期,首先代码是 "多线程" 的. 下面我模拟了项目中的代码写法
package com.home.free.thread;
import java.io.Serializable;
import java.text.SimpleDateFormat;
public class MultiThread {
public static void main(String[] args) {
for (int i = 0; i< 100; i++) {
new Thread(()->{
Demo demo = new Demo();
demo.printDemo(1651766400);
}).start();
new Thread(()->{
Demo demo = new Demo();
demo.printDemo(1651852800);
}).start();
}
}
}
class Demo {
private static SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyyMMdd");
public void printDemo(Integer deliveryDate) {
DemoDto demoDto = new DemoDto();
demoDto.setDeliveryDate(deliveryDate);
String dateStr = simpleDateFormat.format(demoDto.getDeliveryDate()*1000L);
System.out.println(deliveryDate + "-" + dateStr);
}
}
class DemoDto implements Serializable {
private Integer deliveryDate;
public Integer getDeliveryDate() {
return deliveryDate;
}
public void setDeliveryDate(Integer deliveryDate) {
this.deliveryDate = deliveryDate;
}
}
结果 :
1651766400-20220506
1651766400-20220506
1651852800-20220507
1651766400-20220506
1651852800-20220507
1651766400-20220507
1651766400-20220506
1651766400-20220507
1651852800-20220506
1651766400-20220506
1651766400-20220507
1651852800-20220507
1651766400-20220506
1651852800-20220506
1651852800-20220507
1651766400-20220506
1651766400-20220506
1651852800-20220506
1651852800-20220507
1651852800-20220507
1651766400-20220507
1651852800-20220507
1651766400-20220507
1651766400-20220506
1651766400-20220506
1651766400-20220507
1651766400-20220506
1651766400-20220507
1651852800-20220506
1651852800-20220507
1651852800-20220507
1651766400-20220506
1651852800-20220507
1651766400-20220506
1651852800-20220507
1651766400-20220506
1651766400-20220507
1651852800-20220507
1651766400-20220506
1651766400-20220506
1651852800-20220507
1651766400-20220507
1651852800-20220507
1651766400-20220506
1651852800-20220506
1651852800-20220507
1651852800-20220507
1651766400-20220506
1651852800-20220507
1651852800-20220507
1651766400-20220506
1651852800-20220507
1651852800-20220507
1651766400-20220506
1651852800-20220507
1651852800-20220506
1651852800-20220507
1651852800-20220507
1651766400-20220507
1651852800-20220506
1651766400-20220507
1651766400-20220507
1651852800-20220506
1651852800-20220507
1651852800-20220507
1651852800-20220507
1651852800-20220507
1651852800-20220506
1651766400-20220507
1651766400-20220507
1651852800-20220507
1651766400-20220507
1651852800-20220507
1651852800-20220507
1651766400-20220507
1651766400-20220507
1651852800-20220507
1651766400-20220506
1651766400-20220506
1651852800-20220506
1651852800-20220506
1651852800-20220506
1651852800-20220507
1651766400-20220506
1651766400-20220506
1651852800-20220507
1651766400-20220507
1651852800-20220507
1651852800-20220507
1651766400-20220506
1651852800-20220507
1651852800-20220506
1651852800-20220507
1651766400-20220506
1651766400-20220506
1651766400-20220506
1651766400-20220506
1651852800-20220506
1651852800-20220507
1651852800-20220506
1651766400-20220506
1651852800-20220507
1651766400-20220506
1651766400-20220506
1651852800-20220507
1651766400-20220507
1651852800-20220506
1651766400-20220506
1651852800-20220506
1651852800-20220507
1651766400-20220507
1651766400-20220507
1651766400-20220506
1651852800-20220507
1651766400-20220506
1651852800-20220506
1651852800-20220507
1651766400-20220507
1651852800-20220507
1651766400-20220506
1651852800-20220507
1651852800-20220507
1651766400-20220507
1651766400-20220506
1651852800-20220506
1651766400-20220506
1651852800-20220507
1651766400-20220506
1651766400-20220506
1651852800-20220507
1651852800-20220507
1651766400-20220507
1651852800-20220507
1651852800-20220507
1651766400-20220507
1651766400-20220507
1651766400-20220507
1651852800-20220507
1651852800-20220507
1651766400-20220507
1651852800-20220506
1651766400-20220506
1651766400-20220507
1651852800-20220507
1651766400-20220506
1651852800-20220507
1651766400-20220507
1651766400-20220506
1651852800-20220507
1651766400-20220506
1651766400-20220506
1651766400-20220506
1651852800-20220507
1651766400-20220506
1651852800-20220507
1651852800-20220507
1651766400-20220507
1651766400-20220506
1651852800-20220506
1651766400-20220507
1651852800-20220507
1651766400-20220507
1651852800-20220507
1651852800-20220507
1651852800-20220507
1651852800-20220507
1651766400-20220507
1651766400-20220506
1651852800-20220507
1651766400-20220506
1651852800-20220506
1651766400-20220506
1651766400-20220506
1651852800-20220506
1651852800-20220506
1651766400-20220506
1651852800-20220507
1651766400-20220507
1651766400-20220507
1651852800-20220507
1651766400-20220507
1651766400-20220506
1651852800-20220507
1651766400-20220507
1651852800-20220507
1651766400-20220506
1651852800-20220506
1651852800-20220507
1651766400-20220506
1651766400-20220506
1651852800-20220507
1651766400-20220506
1651852800-20220506
1651766400-20220506
1651852800-20220506
1651766400-20220506
1651766400-20220506
1651852800-20220507
1651852800-20220507
1651766400-20220506
结论: 发现多线程情况下,有很多数据是转换错误的。因为simpleDateFormat是多线程的,而创建实体是在类类上,会共享这个数据。
解决:只需将simpleDateFormat的创建对象在自己的线程中创建即可
public void printDemo(Integer deliveryDate) {
DemoDto demoDto = new DemoDto();
demoDto.setDeliveryDate(deliveryDate);
SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyyMMdd");
String dateStr = simpleDateFormat.format(demoDto.getDeliveryDate()*1000L);
System.out.println(deliveryDate + "-" + dateStr);
}