Java程序将LocalDateTime转换为java.util.Date
将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.271973ZDate = Fri Apr 19 10:04:31 IST 2019
以上是 Java程序将LocalDateTime转换为java.util.Date 的全部内容, 来源链接: utcz.com/z/317054.html