一.在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的更多相关文章

  1. Java8中 Date和LocalDateTime的相互转换

    一.在Java 8中将Date转换为LocalDateTime 方法1: 将Date转换为LocalDatetime,我们可以使用以下方法: 1.从日期获取ZonedDateTime并使用其方法toL ...

  2. Java8 LocalDateTime获取时间戳(毫秒/秒)、LocalDateTime与String互转、Date与LocalDateTime互转

    本文目前提供:LocalDateTime获取时间戳(毫秒/秒).LocalDateTime与String互转.Date与LocalDateTime互转 文中都使用的时区都是东8区,也就是北京时间.这是 ...

  3. Java日期时间API系列13-----Jdk8中java.time包中的新的日期时间API类,时间类转换,Date转LocalDateTime,LocalDateTime转Date等

    从前面的系列博客中可以看出Jdk8中java.time包中的新的日期时间API类设计的很好,但Date由于使用仍非常广泛,这就涉及到Date转LocalDateTime,LocalDateTime转D ...

  4. JSONObject转换JSON之将Date转换为指定格式(转)

    项目中,经常会用JSONObject插件将JavaBean或List<JavaBean>转换为JSON格式的字符串,而JavaBean的属性有时候会有java.util.Date这个类型的 ...

  5. Java 8 中 Date与LocalDateTime、LocalDate、LocalTime互转

    Java 8中 java.util.Date 类新增了两个方法,分别是from(Instant instant)和toInstant()方法 // Obtains an instance of Dat ...

  6. Convert Date between LocalDateTime

    http://blog.progs.be/542/date-to-java-time Java8 has new date and time classes to “replace” the old ...

  7. jdk8中奖Date转换为String格式的方法

    public static String getLocalDateStr(Date date,String formatter) { DateTimeFormatter dateTimeFormatt ...

  8. Date以及LocalDateTime格式化

    public static void main(String[] args) { LocalDateTime local = LocalDateTime.now(); Date date = new ...

  9. 在EL表达式或者Struts标签库中格式化日期对象,即将Date转换为yyyy-MM-dd格式

    一.EL表达式 首先,在jsp页面引入<fmt> tags,<%@ taglib prefix="fmt" uri="http://java.sun.c ...

随机推荐

  1. nginx,php-fpm性能优化

    When you running a highload website with PHP-FPM via FastCGI, the following tips may be useful to yo ...

  2. iOS: NSTimer使用小记

    目录 1. NSRunLoopCommonModes和Timer 2. NSThread和Timer 3. GCD中的Timer 返回目录 1. NSRunLoopCommonModes和Timer ...

  3. 资源-.Net-ASP.NET:ASP.NET资源列表

    ylbtech-资源-.Net-ASP.NET:ASP.NET资源列表 ASP.NETFree. Cross-platform. Open source.A framework for buildin ...

  4. JUC源码分析-集合篇:并发类容器介绍

    JUC源码分析-集合篇:并发类容器介绍 同步类容器是 线程安全 的,如 Vector.HashTable 等容器的同步功能都是由 Collections.synchronizedMap 等工厂方法去创 ...

  5. c# mvc 简洁大气官网---源码

    结构

  6. dnslog小技巧

    一.dnslog利用场景 主要针对无回显的情况. Sql-Blind RCE SSRF RFI(Remote File Inclusion) 二.原理 将dnslog平台中的特有字段payload带入 ...

  7. API Gateway和Route 53及CloudFront的连携使用

    API Gateway部署出来之后的url网址对于普通用户并不友好,所以肯定是需要一个正常的域名来作为url进行访问. 主要有以下几点, API Gateway可以自定义域名 自定义的域名要从Rout ...

  8. Laravel 项目运行 phpunit 测试结果只显示点号

    在laravel 项目的根目录下,运行 phpunit 只显示 点号的情况 我尝试将 tests/Unit 和 tests/Feature 目录将 ExampleTest.php 文件删除,然后再运行 ...

  9. 强大的httpClientUtils

    <!-- https://mvnrepository.com/artifact/com.arronlong/httpclientutil --> <dependency> &l ...

  10. CentOS7-安装最新版本GIT(git version 2.18.0)

    Git安装方式有两种一种是yum安装一种是编译安装: 一.yum命令安装,此方法简单,会自动安装依赖的包,而且会从源里安装最新的版本,如果仓库不是最新的话安装的也不是最新Git. sudo yum i ...