0. 前言
- 若对jdk8的时间操作还不了解的,可以先看这篇文章:
https://blog.csdn.net/xueyijin/article/details/121736594
1. java.sql.Date 转化为 LocalDate
public class demo {
public static void main(String[] args) {
java.sql.Date date = new Date(System.currentTimeMillis());
System.out.println("java.sql.Date date: " + date);
System.out.println("转化后:" + date.toLocalDate());
}
}
2. java.sql.Timestamp 转化为 LocalDateTime
public class demo {
public static void main(String[] args) {
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
System.out.println(timestamp);
System.out.println("转化后:" + timestamp.toLocalDateTime());
}
}
3. java.util.Date 转化为 LocalDate
1. 使用Instant类(时间戳)作为中介 转化为 LocalDate
public class demo {
public static void main(String[] args) {
java.util.Date date = new Date(System.currentTimeMillis());
System.out.println(date);
Instant instant = date.toInstant();
final ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());
System.out.println("转化后:" + zonedDateTime.toLocalDate());
}
}
2. 使用java.sql.Date 作为中介 转化为 LocalDate
public class demo {
public static void main(String[] args) {
java.util.Date date = new Date(System.currentTimeMillis());
System.out.println(date);
final java.sql.Date date2 = new java.sql.Date(date.getTime());
System.out.println("转化后:" + date2.toLocalDate());
}
}
4. java.util.Date 转化为 LocalDateTime
public class demo {
public static void main(String[] args) {
Date date = new Date(System.currentTimeMillis());
System.out.println(date);
Timestamp timestamp = new Timestamp(date.getTime());
System.out.println("转化后:" + timestamp.toLocalDateTime());
}
}