java new Date 生日日期的坑,中国夏令时区转换

it2025-03-20  20

问题描述

最近在开发有关生日的字段时发现,在特定的时间里,返回给前端的日期数据会少一天,找到根本原因是在1986年到1991年的6年间,我国使用的夏令时时区。造成时间会有一个小时的差别。反馈到生日的日期上就会少一天

问题处理

@GetMapping("/test") public PfProposalVehicleOwnerDto test() throws ParseException { String date = "1988-05-03"; PfProposalVehicleOwnerDto dto = new PfProposalVehicleOwnerDto(); ZoneId zoneId = ZoneId.systemDefault(); DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyy-MM-dd"); LocalDate localDate = LocalDate.parse(date, ofPattern); ZonedDateTime zdt = localDate.atStartOfDay(zoneId); dto.setBirthDate(Date.from(zdt.toInstant())); return dto; }
最新回复(0)