Java 程序将 java.util.Date 转换为 java.time.LocalDateTime
首先,设置日期 −
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
广告