首先,设置日期-
java.util.Date date = new Date();
现在,将上述日期转换为java.time.LocalDateTime-
java.time.LocalDateTime dateTime = java.time.LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
import java.time.ZoneId; import java.util.Date; public class Demo { public static void main(String[] args) { java.util.Date date = new Date(); System.out.println("Date = "+date); java.time.LocalDateTime dateTime = java.time.LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); System.out.println("LocalDateTime = "+dateTime); } }
输出结果
Date = Thu Apr 18 23:39:34 IST 2019 LocalDateTime = 2019-04-18T23:39:34.400