Date转换为LocalDateTime
一.在Java 8中将Date转换为LocalDateTime
方法1:
将Date转换为LocalDatetime,我们可以使用以下方法:
1.从日期获取ZonedDateTime并使用其方法toLocalDateTime()获取LocalDateTime
2.使用LocalDateTime的Instant()工厂方法
示例:
package insping;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
public class Test {
public static void main(String[] args) {
Date date = new Date();
Instant instant = date.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
System.out.println("Date = " + date);
System.out.println("LocalDateTime = " + localDateTime);
}
}
结果:
Date = Fri Jun 16 15:35:26 CST 2017
LocalDateTime = 2017-06-16T15:35:26.970
方法2:
我们也可以使用LocalDateTime的FactoryInput()方法使用系统的默认时区。
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), zoneId);
二.在Java 8中将LocalDateTime转换为Date
要将LocalDateTime转换回java.util.Date,我们可以使用以下步骤:
1.使用atZone()方法将LocalDateTime转换为ZonedDateTime
2.将ZonedDateTime转换为Instant,并从中获取Date
示例:
package insping;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
public class Test {
public static void main(String[] args) {
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.now();
ZonedDateTime zdt = localDateTime.atZone(zoneId);
Date date = Date.from(zdt.toInstant());
System.out.println("LocalDateTime = " + localDateTime);
System.out.println("Date = " + date);
}
}
结果:
LocalDateTime = 2017-06-16T15:38:48.580
Date = Fri Jun 16 15:38:48 CST 2017
原文地址:https://blog.csdn.net/hspingcc/article/details/73332380
Date转换为LocalDateTime的更多相关文章
- Java8中 Date和LocalDateTime的相互转换
一.在Java 8中将Date转换为LocalDateTime 方法1: 将Date转换为LocalDatetime,我们可以使用以下方法: 1.从日期获取ZonedDateTime并使用其方法toL ...
- Java8 LocalDateTime获取时间戳(毫秒/秒)、LocalDateTime与String互转、Date与LocalDateTime互转
本文目前提供:LocalDateTime获取时间戳(毫秒/秒).LocalDateTime与String互转.Date与LocalDateTime互转 文中都使用的时区都是东8区,也就是北京时间.这是 ...
- Java日期时间API系列13-----Jdk8中java.time包中的新的日期时间API类,时间类转换,Date转LocalDateTime,LocalDateTime转Date等
从前面的系列博客中可以看出Jdk8中java.time包中的新的日期时间API类设计的很好,但Date由于使用仍非常广泛,这就涉及到Date转LocalDateTime,LocalDateTime转D ...
- JSONObject转换JSON之将Date转换为指定格式(转)
项目中,经常会用JSONObject插件将JavaBean或List<JavaBean>转换为JSON格式的字符串,而JavaBean的属性有时候会有java.util.Date这个类型的 ...
- Java 8 中 Date与LocalDateTime、LocalDate、LocalTime互转
Java 8中 java.util.Date 类新增了两个方法,分别是from(Instant instant)和toInstant()方法 // Obtains an instance of Dat ...
- Convert Date between LocalDateTime
http://blog.progs.be/542/date-to-java-time Java8 has new date and time classes to “replace” the old ...
- jdk8中奖Date转换为String格式的方法
public static String getLocalDateStr(Date date,String formatter) { DateTimeFormatter dateTimeFormatt ...
- Date以及LocalDateTime格式化
public static void main(String[] args) { LocalDateTime local = LocalDateTime.now(); Date date = new ...
- 在EL表达式或者Struts标签库中格式化日期对象,即将Date转换为yyyy-MM-dd格式
一.EL表达式 首先,在jsp页面引入<fmt> tags,<%@ taglib prefix="fmt" uri="http://java.sun.c ...
随机推荐
- Shiro学习(6)Realm整合
6.1 Realm [2.5 Realm]及[3.5 Authorizer]部分都已经详细介绍过Realm了,接下来再来看一下一般真实环境下的Realm如何实现. 1.定义实体及关系 即用户-角色之间 ...
- BZOJ 3252: 攻略(思路题)
传送门 解题思路 比较好想的一道思路题,结果有个地方没开\(long\) \(long\) \(wa\)了三次..其实就是模仿一下树链剖分,重新定义重儿子,一个点的重儿子为所有儿子中到叶节点权值最大的 ...
- HTTPS 加密机制
目录 1. HTTPS 概述 2. 对称加密 3. 非对称加密 4. 非对称加密改良方案 5. 非对称加密 + 对称加密 6. 中间人攻击 7. 数字证书 8. 数字签名 9. HTTPS 工作原理 ...
- 7、Appium常用API
嗯,官网已经介绍的很全了.会选几个常用API后期整理. Appium常用API地址:http://appium.io/docs/cn/writing-running-appium/appium-bin ...
- centos7升级php5.4到php5.6
history命令历史 8 yum provides php #自带的只有5.4版本 9 rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-relea ...
- 7-vim-移动命令-02-行数跳转和上下翻页
1.行数跳转 命令 英文 功能 gg go 文件顶部 G GO 文件尾部 数字gg 移动到数字对应行数 数字G 移动到数字对应行数 :数字 移动到数字对应行数 2.屏幕移动 命令 英文 功 ...
- KMP2
poj 2752 #include<stdio.h> #include<iostream> #include<algorithm> #include<stri ...
- vue中:key 和react 中key={} 的作用,以及ref的特性?
vue中:key 和react 中key={} 为了给 vue 或者react 一个提示,以便它能跟踪每个节点的身份,从而重用和重新排序现有元素,你需要为每项提供一个唯一 key 属性 一句话概括就是 ...
- 不走弯路,微信小程序的快速入门?
微信小程序下载注册地址:https://mp.weixin.qq.com/cgi-bin/wx 微信小程序登开发者平台地址(可以查看小程序APPID):https://mp.weixin.qq.com ...
- tomcat启动内存修改
# USE_NOHUP (Optional) If set to the string true the start command will # ...