首先,设置日期-
java.util.Date date = new java.util.Date();
现在,将上述日期转换为Instant-
Instant instant = date.toInstant();
import java.time.Instant; public class Demo { public static void main(String[] args) { java.util.Date date = new java.util.Date(); System.out.println("Date = "+date); Instant instant = date.toInstant(); System.out.println("java.util.Date to Instant = "+instant); } }
输出结果
Date = Thu Apr 18 23:32:07 IST 2019 java.util.Date to Instant = 2019-04-18T18:02:07.330Z