If you directly use LocalDateTime.parse to convert the date string (yyyy-MM-dd) to LocalDateTime, it will cause an error, so I provide a method to convert the date string to LocalDateTime for reference only. If there is a better way, you are welcome to share.
public static final String DATE_FORMAT = "yyyy-MM-dd";
public static LocalDateTime parserString2LocalDateTime(final String dateTime, final String formatter) {
if (StringUtils.isBlank(dateTime) || IBusinessEntity.NULL_VALUE.equalsIgnoreCase(dateTime)) {
return null;
}
if(dateTime.length() == 10){
return convertDate2LocalDateTime(convertLocalDate2Date(parserString2LocalDate(dateTime)));
}
return LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern(formatter));
}
public static LocalDate parserString2LocalDate(final String date) {
return parserString2LocalDate(date, DATE_FORMAT);
}
public static LocalDateTime convertDate2LocalDateTime(Date date) {
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
return localDateTime;
}