如何转换sql日期格式?

问题描述

您好,我正在尝试将 sql 日期格式转换为“dd-MM-yyyy”,但在输出我有这种不需要的格式,它显示了当前日期时间(星期一 8 月 14 日 00.00....)

String inputDate  = rs.getDate("BIRTH_DATE").toString();
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
Date outputDate = formatter.parse(inputDate);
user.setBirthdate(outputDate);

解决方法

日期没有固有格式。

java.util.Date inputDate = rs.getDate("BIRTH_DATE");
user.setBirthdate(inputDate);

SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String representation = formatter.format(inputDate);

最好使用新的 java.time 类:LocalDate,ZonedDateTime 等。