爪哇 LocalDateTime到日期

In this post, we will see how to convert LocalDateTime
to 日期
. 爪哇 8 为日期和时间引入了许多新的API。
There can be many ways to convert 爪哇 LocalDateTime
to date
.
使用即时对象
You can convert LocalDateTime to date using Instant
object which we can 从 ZonedDateTime
. Here is the code for the same:
1 2 3 4 5 6 7 |
日期 convertLocalDateTimeToDateUsingInstant(LocalDateTime dateToConvert) { 返回 爪哇.实用程序.日期 .从(dateToConvert.atZone(ZoneId.系统默认()) .即时()); } |
使用时间戳
这是将LocalDateTime转换为日期的最简单方法 爪哇 8. We can use 爪哇.sql.Timestamp
to convert LocalDateTime
to 日期
.
The easiest way of getting a 爪哇.util.Date 从 LocalDateTime is to use an extension to the 爪哇.sql.Timestamp
– available with 爪哇 8:
1 2 3 4 5 |
上市 日期 convertLocalDateTimeToDateUsingTimestamp(LocalDateTime dateToConvert) { 返回 爪哇.sql.时间戳记.的价值(dateToConvert); } |
这是在LocalDateTime和Date之间转换的完整程序。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
包 组织.Arpit.爪哇2blog; 进口 爪哇.sql.时间戳记; 进口 爪哇.时间.LocalDateTime; 进口 爪哇.时间.ZoneId; 进口 爪哇.实用程序.日期; 上市 类 LocalDateTimeToDateMain { 上市 静态的 虚空 主要(串[] args) { LocalDateTime ldt = LocalDateTime.现在(); 日期 dt1=convertLocalDateTimeToDateUsingInstant(ldt); 系统.出.打印(dt1); 系统.出.打印(“ =====================”); 日期 dt2=convertLocalDateTimeToDateUsingTimestamp(ldt); 系统.出.打印(dt2); } 上市 静态的 日期 convertLocalDateTimeToDateUsingInstant(LocalDateTime localDateTime) { 返回 日期 .从(localDateTime.atZone(ZoneId.系统默认()) .即时()); } 上市 静态的 日期 convertLocalDateTimeToDateUsingTimestamp(LocalDateTime localDateTime) { 返回 时间戳记.的价值(localDateTime); } } |
输出:
=====================
2019-04-14 00:47:05.179772
那’关于竞彩篮球分析 LocalDateTime到日期的全部内容 爪哇 8.