我想将java.time.LocalDate转换为java.util.Date类型。因为我想将日期设置为JDateChooser。或者有任何日期选择器支持java。时间日期吗?


当前回答

简单的

public Date convertFrom(LocalDate date) {
    return java.sql.Timestamp.valueOf(date.atStartOfDay());
}

其他回答

    LocalDate date = LocalDate.now();
    DateFormat formatter = new SimpleDateFormat("dd-mm-yyyy");
    try {
        Date utilDate= formatter.parse(date.toString());
    } catch (ParseException e) {
        // handle exception
    }

试试这个:

public Date convertFrom(LocalDate date) {
    return Date.valueOf(date);
}
localDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
public static Date convertToTimeZone(Date date, String tzFrom, String tzTo) {
    return Date.from(LocalDateTime.ofInstant(date.toInstant(), ZoneId.of(tzTo)).atZone(ZoneId.of(tzFrom)).toInstant());
} 
Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());

这假设日期选择器使用系统默认时区将日期转换为字符串。