BeanUtil复制时,两对象中数据类型不一致导致的问题Can not set java.time.LocalDateTime field to java.lang.Long
@Data public class AVo { private Long endTime; private String name; private String id; }
@Data public class ABVo { private LocalDateTime endTime; private String name; private String id; }
AVo aVo = new AVo(); ABVo bVo = new ABVo(); aVo.setEndTime(1720141535793L); aVo.setName("李逵"); aVo.setId("sdd"); Long sourceEndTime = aVo.getEndTime(); LocalDateTime targetEndTime = sourceEndTime != null ? LocalDateTime.ofInstant(Instant.ofEpochMilli(sourceEndTime), ZoneId.systemDefault()) : null; bVo.setEndTime(targetEndTime);
//不好使,endTime类型不一致,直接导致
// BeanUtil.copyPropertiesByField(aVo, bVo);
// 此处为核心代码
CopyOptions copyOptions = CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true); BeanUtil.copyProperties(aVo, bVo,copyOptions);
System.out.println(bVo.getEndTime()); System.out.println(bVo.getName());