将LocalDateTime设置为当前日期时间-
LocalDateTime dateTime = LocalDateTime.now();
创建一个即时-
Instant i = dateTime.atZone(ZoneId.systemDefault()).toInstant(); Convert LocalDateTime to java.util.Date: java.util.Date date = Date.from(i);
import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Date; public class Demo { public static void main(String[] args) { LocalDateTime dateTime = LocalDateTime.now(); Instant i = dateTime.atZone(ZoneId.systemDefault()).toInstant(); System.out.println("Instant = "+i); java.util.Date date = Date.from(i); System.out.println("Date = "+date); } }
输出结果
Instant = 2019-04-19T04:34:31.271973Z Date = Fri Apr 19 10:04:31 IST 2019