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 ...
随机推荐
- (转)OpenFire源码学习之十三:消息处理
转:http://blog.csdn.net/huwenfeng_2011/article/details/43417817 消息处理流程总揽(该图来源于互联网,图片很大,不过类容还是挺清楚的.不方便 ...
- msgbox用法
http://www.w3school.com.cn/vbscript/func_msgbox.asp VBScript MsgBox 函数 VBScript 函数参考手册 定义和用法 MsgBox ...
- MarkDown 快速开始 基础教学
# MarkDown 快速上手 # > [源代码](https://www.cnblogs.com/qiyuexin/p/9932941.html) > by qyx@2018/11/07 ...
- Codeforces 340B - Maximal Area Quadrilateral (计算几何)
Codeforces Round #198 (Div. 2) 题目链接:Maximal Area Quadrilateral Iahub has drawn a set of \(n\) points ...
- 作用域 {}代码块 const修饰符 引用
简单分为:全局作用域.局部作用域.语句作用域 如果希望在局部变量的作用域内使用同名的全局变量,可以在该变量前加上“::” ::aver=20 #include<iostream> usin ...
- 使用target属性跳转到指定位置
先上代码,使用frameset将网页分割为三个窗口,上,左和右. 1 <html> 2 <head> 3 <title>Main</title> 4 & ...
- Mac版本的 Axure rp8 不显示菜单栏
我之前也是一直在找这个问题,可能mac用的不熟练吧,其实他的菜单栏就近在眼前 你看不见只是因为你的关注点在axure上 往大了看,他的菜单栏显示在你的电脑的菜单栏上,mac的菜单栏基本都是这么显示的, ...
- 2018-2-13-win10-uwp-iot
title author date CreateTime categories win10 uwp iot lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:2 ...
- Go学习笔记:初识Go语言
Go语言简介 Go语言是Google(谷歌)公司开发的一款静态型.编译型并自带垃圾回收机制和并发的编程语言. Go语言的风格类似于C语言.其语法在C语言的基础上进行了大幅的简化,去掉了不需要的表达式括 ...
- 读书笔记五--numpy
数组转置和轴对换 转置是重塑的一种特殊形式,返回的是源数据的视图(不会进行任何复制操作).数组不仅有transpose方法,还有一个特殊的T属性: arr=np.arange().reshape((, ...