Java程序将LocalDate转换为java.util.Date
我们首先将LocalDate设置为当前日期-
LocalDate date = LocalDate.now();
创建一个即时-
Instant i = date.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
将其转换为java.util.Date-
java.util.Date.from(i)
示例
import java.time.Instant; import java.time.LocalDate; import java.time.ZoneId; public class Demo { public static void main(String[] args) { LocalDate date = LocalDate.now(); System.out.println("LocalDate = "+date); Instant i = date.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant(); System.out.println("java.util.Date = "+java.util.Date.from(i)); } }
输出结果
LocalDate = 2019-04-19 java.util.Date = Fri Apr 19 00:00:00 IST 2019