爪哇日期到LocalDate

在这篇文章中,我们将看到如何在Java中将Date转换为LocalDate。
有时,我们可能需要将Date转换为新的Java 8 API,反之亦然。在Java中,有多种将Date转换为LocalDate的方法。
使用Date类的toInstant()方法
You can convert Date to LocalDate using 即时()
method of Date class. Since 立即的
objects are time agnostic, you need to use atZone()
method to convert to derive 本地日期
from it.
这是一个例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
包裹 组织.Arpit.爪哇2blog.入口; 进口 爪哇.时间.本地日期; 进口 爪哇.时间.ZoneId; 进口 爪哇.效用.日期; 上市 班级 爪哇DateToLocalDateUsingInstant { 上市 静态的 空白 主要的(细绳[] args) { 日期 日期=新的 日期(); 本地日期 localDate = convertDateToLocalDateUsingInstant(日期); 系统.出去.打印(“本地日期:”+localDate); } 上市 静态的 本地日期 convertDateToLocalDateUsingInstant(日期 日期) { 返回 日期.即时() .atZone(ZoneId.系统默认()) .到LocalDate(); } } |
输出:
使用Date类的toInstant()方法
You can convert Date to LocalDate using 即时()
method of 日期
class. You can use Intant.ofEpochMilli(date.getTime())
to derive instant object and use atZone()
method to associate time zone to instant object.
这是一个例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
包裹 组织.Arpit.爪哇2blog.入口; 进口 爪哇.时间.立即的; 进口 爪哇.时间.本地日期; 进口 爪哇.时间.ZoneId; 进口 爪哇.效用.日期; 上市 班级 爪哇DateToLocalDateUsingOfEpochMilli { 上市 静态的 空白 主要的(细绳[] args) { 日期 日期=新的 日期(); 本地日期 localDate = convertDateToLocalDateUsingOfEpochMilli(日期); 系统.出去.打印(“本地日期:”+localDate); } 上市 静态的 本地日期 convertDateToLocalDateUsingOfEpochMilli(日期 日期) { 返回 立即的.史密斯学院(日期.getTime()) .atZone(ZoneId.系统默认()) .到LocalDate(); } } |
输出:
使用java.sql.Date
You can convert Date to LocalDate using 爪哇.sql.Date
. In Java 8, there is new method 到LocalDate()
added to 爪哇.sql.Date
class.`
这是一个例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
包裹 组织.Arpit.爪哇2blog.入口; 进口 爪哇.时间.本地日期; 进口 爪哇.效用.日期; 上市 班级 爪哇DateToLocalDateUsingSQLDate { 上市 静态的 空白 主要的(细绳[] args) { 日期 日期=新的 日期(); 本地日期 localDate = convertDateToLocalDateUsingSQLDate(日期); 系统.出去.打印(“本地日期:”+localDate); } 上市 静态的 本地日期 convertDateToLocalDateUsingSQLDate(日期 日期) { 返回 新的 爪哇.sql.日期(日期.getTime()).到LocalDate(); } } |
输出:
那’关于如何在Java中将Date转换为LocalDate的所有内容