Java日期时间API系列13

Wesley13
• 阅读 596

  从前面的系列博客中可以看出Jdk8中java.time包中的新的日期时间API类设计的很好,但Date由于使用仍非常广泛,这就涉及到Date转LocalDateTime,LocalDateTime转Date。下面是时间类互相转换大全,包含Instant、LocalDate、LocalDateTime、LocalTime、ZonedDateTime和Date的相互转换,时间转换大全,下面是一个工具类,仅供参考:

具体包含:

LocalDateTime转Date,LocalDate转Date,LocalTime转Date,Instant转Date,epochMilli毫秒转Date,ZonedDateTime转Date,Date转LocalDateTime,LocalDate转LocalDateTime,LocalTime转LocalDateTime,Instant转LocalDateTime,epochMilli毫秒转LocalDateTime,temporal转LocalDateTime,ZonedDateTime转LocalDateTime,Date转LocalDate,LocalDateTime转LocalDate,Instant转LocalDate,temporal转LocalDate,ZonedDateTime转LocalDate,Date转LocalTime,LocalDateTime转LocalTime,Instant转LocalTime,temporal转LocalTime,ZonedDateTime转LocalTime,Date转Instant,LocalDateTime转Instant,LocalDate转Instant,LocalTime转Instant,epochMilli毫秒转Instant,temporal转Instant,ZonedDateTime转Instant,Date转毫秒值,LocalDateTime转毫秒值,LocalDate转毫秒值,Instant转毫秒值,ZonedDateTime转毫秒值,Date转ZonedDateTime,LocalDateTime转ZonedDateTime,LocalDate转ZonedDateTime,LocalTime转ZonedDateTime,Instant转ZonedDateTime,epochMilli毫秒转ZonedDateTime,temporal转ZonedDateTime.

package com.xkzhangsan.time.converter;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalAccessor;
import java.util.Date;
import java.util.Objects;

/**
 * 日期转换
 * 包含Date、LocalDate、LocalDateTime、LocalTime、Instant和ZonedDateTime的互相转换
 * 
 * 注意,ZonedDateTime相关的转换,尤其是其他时间转ZonedDateTime,要注意时间和对应时区一致。
* @ClassName: DateTimeConverterUtil 
* @Description: DateTime Converter
* @author xkzhangsan
* @date 2019年12月1日
*
 */
public class DateTimeConverterUtil {
    
    private DateTimeConverterUtil(){
    }

    /**
     * LocalDateTime转Date
     * @param localDateTime
     * @return
     */
    public static Date toDate(LocalDateTime localDateTime) {
        Objects.requireNonNull(localDateTime, "localDateTime");
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * LocalDate转Date
     * @param localDate
     * @return
     */
    public static Date toDate(LocalDate localDate) {
        Objects.requireNonNull(localDate, "localDate");
        return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }
    
    /**
     * LocalTime转Date
     * 以当天的日期+LocalTime组成新的LocalDateTime转换为Date
     * @param localTime
     * @return
     */
    public static Date toDate(LocalTime localTime) {
        Objects.requireNonNull(localTime, "localTime");
        return Date.from(LocalDate.now().atTime(localTime).atZone(ZoneId.systemDefault()).toInstant());
    }    

    /**
     * Instant转Date
     * @param instant
     * @return
     */
    public static Date toDate(Instant instant) {
        return Date.from(instant);
    }
    
    /**
     * epochMilli毫秒转Date
     * @param epochMilli
     * @return
     */
    public static Date toDate(long epochMilli){
        Objects.requireNonNull(epochMilli, "epochMilli");
        return new Date(epochMilli);
    }
    
    /**
     * ZonedDateTime转Date
     * 注意时间对应的时区和默认时区差异
     * @param zonedDateTime
     * @return
     */
    public static Date toDate(ZonedDateTime zonedDateTime) {
        Objects.requireNonNull(zonedDateTime, "zonedDateTime");
        return Date.from(zonedDateTime.toInstant());
    }

    /**
     * Date转LocalDateTime
     * @param date
     * @return
     */
    public static LocalDateTime toLocalDateTime(Date date) {
        Objects.requireNonNull(date, "date");
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    /**
     * LocalDate转LocalDateTime
     * @param localDate
     * @return
     */
    public static LocalDateTime toLocalDateTime(LocalDate localDate) {
        Objects.requireNonNull(localDate, "localDate");
        return localDate.atStartOfDay();
    }
    
    /**
     * LocalTime转LocalDateTime
     * 以当天的日期+LocalTime组成新的LocalDateTime
     * @param localTime
     * @return
     */
    public static LocalDateTime toLocalDateTime(LocalTime localTime) {
        Objects.requireNonNull(localTime, "localTime");
        return LocalDate.now().atTime(localTime);
    }

    /**
     * Instant转LocalDateTime
     * @param instant
     * @return
     */
    public static LocalDateTime toLocalDateTime(Instant instant) {
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    }
    
    /**
     * epochMilli毫秒转LocalDateTime
     * @param epochMilli
     * @return
     */
    public static LocalDateTime toLocalDateTime(long epochMilli) {
        Objects.requireNonNull(epochMilli, "epochMilli");
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneId.systemDefault());
    }
    
    /**
     * temporal转LocalDateTime
     * @param temporal
     * @return
     */
    public static LocalDateTime toLocalDateTime(TemporalAccessor temporal) {
        return LocalDateTime.from(temporal);
    }
    
    /**
     * ZonedDateTime转LocalDateTime
     * 注意时间对应的时区和默认时区差异
     * @param zonedDateTime
     * @return
     */
    public static LocalDateTime toLocalDateTime(ZonedDateTime zonedDateTime) {
        Objects.requireNonNull(zonedDateTime, "zonedDateTime");
        return zonedDateTime.toLocalDateTime();
    }

    /**
     * Date转LocalDate
     * @param date
     * @return
     */
    public static LocalDate toLocalDate(Date date) {
        return toLocalDateTime(date).toLocalDate();
    }

    /**
     * LocalDateTime转LocalDate
     * @param localDateTime
     * @return
     */
    public static LocalDate toLocalDate(LocalDateTime localDateTime) {
        Objects.requireNonNull(localDateTime, "localDateTime");
        return localDateTime.toLocalDate();
    }

    /**
     * Instant转LocalDate
     * @param instant
     * @return
     */
    public static LocalDate toLocalDate(Instant instant) {
        return toLocalDateTime(instant).toLocalDate();
    }
    
    /**
     * temporal转LocalDate
     * @param temporal
     * @return
     */
    public static LocalDate toLocalDate(TemporalAccessor temporal) {
        return LocalDate.from(temporal);
    }
    
    /**
     * ZonedDateTime转LocalDate
     * 注意时间对应的时区和默认时区差异
     * @param zonedDateTime
     * @return
     */
    public static LocalDate toLocalDate(ZonedDateTime zonedDateTime) {
        Objects.requireNonNull(zonedDateTime, "zonedDateTime");
        return zonedDateTime.toLocalDate();
    }

    /**
     * Date转LocalTime
     * @param date
     * @return
     */
    public static LocalTime toLocalTime(Date date) {
        return toLocalDateTime(date).toLocalTime();
    }

    /**
     * LocalDateTime转LocalTime
     * @param localDateTime
     * @return
     */
    public static LocalTime toLocalTime(LocalDateTime localDateTime) {
        Objects.requireNonNull(localDateTime, "localDateTime");
        return localDateTime.toLocalTime();
    }

    /**
     * Instant转LocalTime
     * @param instant
     * @return
     */
    public static LocalTime toLocalTime(Instant instant) {
        return toLocalDateTime(instant).toLocalTime();
    }
    
    /**
     * temporal转LocalTime
     * @param temporal
     * @return
     */
    public static LocalTime toLocalTime(TemporalAccessor temporal) {
        return LocalTime.from(temporal);
    }
    
    /**
     * ZonedDateTime转LocalTime
     * 注意时间对应的时区和默认时区差异
     * @param zonedDateTime
     * @return
     */
    public static LocalTime toLocalTime(ZonedDateTime zonedDateTime) {
        Objects.requireNonNull(zonedDateTime, "zonedDateTime");
        return zonedDateTime.toLocalTime();
    }

    /**
     * Date转Instant
     * @param date
     * @return
     */
    public static Instant toInstant(Date date) {
        Objects.requireNonNull(date, "date");
        return date.toInstant();
    }

    /**
     * LocalDateTime转Instant
     * @param localDateTime
     * @return
     */
    public static Instant toInstant(LocalDateTime localDateTime) {
        Objects.requireNonNull(localDateTime, "localDateTime");
        return localDateTime.atZone(ZoneId.systemDefault()).toInstant();
    }

    /**
     * LocalDate转Instant
     * @param localDate
     * @return
     */
    public static Instant toInstant(LocalDate localDate) {
        return toLocalDateTime(localDate).atZone(ZoneId.systemDefault()).toInstant();
    }
    
    /**
     * LocalTime转Instant
     * 以当天的日期+LocalTime组成新的LocalDateTime转换为Instant
     * @param localTime
     * @return
     */
    public static Instant toInstant(LocalTime localTime) {
        return toLocalDateTime(localTime).atZone(ZoneId.systemDefault()).toInstant();
    }
    
    /**
     * epochMilli毫秒转Instant
     * @param epochMilli
     * @return
     */
    public static Instant toInstant(long epochMilli) {
        Objects.requireNonNull(epochMilli, "epochMilli");
        return Instant.ofEpochMilli(epochMilli);
    }
    
    /**
     * temporal转Instant
     * @param temporal
     * @return
     */
    public static Instant toInstant(TemporalAccessor temporal) {
        return Instant.from(temporal);
    }
    
    /**
     * ZonedDateTime转Instant
     * 注意时间对应的时区和默认时区差异
     * @param zonedDateTime
     * @return
     */
    public static Instant toInstant(ZonedDateTime zonedDateTime) {
        Objects.requireNonNull(zonedDateTime, "zonedDateTime");
        return zonedDateTime.toInstant();
    }
    
    /**
     * Date转毫秒值
     * 从1970-01-01T00:00:00Z开始的毫秒值
     * @param date
     * @return
     */
    public static long toEpochMilli(Date date){
        Objects.requireNonNull(date, "date");
        return date.getTime();
    }
    
    /**
     * LocalDateTime转毫秒值
     * 从1970-01-01T00:00:00Z开始的毫秒值
     * @param localDateTime
     * @return
     */
    public static long toEpochMilli(LocalDateTime localDateTime){
        return toInstant(localDateTime).toEpochMilli();
    }
    
    /**
     * LocalDate转毫秒值
     * 从1970-01-01T00:00:00Z开始的毫秒值
     * @param localDate
     * @return
     */
    public static long toEpochMilli(LocalDate localDate){
        return toInstant(localDate).toEpochMilli();
    }
    
    /**
     * Instant转毫秒值
     * 从1970-01-01T00:00:00Z开始的毫秒值
     * @param instant
     * @return
     */
    public static long toEpochMilli(Instant instant){
        Objects.requireNonNull(instant, "instant");
        return instant.toEpochMilli();
    }
    
    /**
     * ZonedDateTime转毫秒值,注意时间对应的时区和默认时区差异
     * 从1970-01-01T00:00:00Z开始的毫秒值
     * @param zonedDateTime
     * @return
     */
    public static long toEpochMilli(ZonedDateTime zonedDateTime) {
        Objects.requireNonNull(zonedDateTime, "zonedDateTime");
        return zonedDateTime.toInstant().toEpochMilli();
    }
    
    /**
     * Date转ZonedDateTime,时区为系统默认时区
     * @param date
     * @return
     */
    public static ZonedDateTime toZonedDateTime(Date date) {
        Objects.requireNonNull(date, "date");
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime()
                .atZone(ZoneId.systemDefault());
    }
    
    /**
     * LocalDateTime转ZonedDateTime,时区为系统默认时区
     * @param localDateTime
     * @return
     */
    public static ZonedDateTime toZonedDateTime(LocalDateTime localDateTime) {
        Objects.requireNonNull(localDateTime, "localDateTime");
        return localDateTime.atZone(ZoneId.systemDefault());
    }

/**
* LocalDateTime转ZonedDateTime,时区为zoneId对应时区
* 注意,需要保证localDateTime和zoneId是对应的,不然会出现错误
*
* @param localDateTime
* @param zoneId
* @return
*/
public static ZonedDateTime toZonedDateTime(LocalDateTime localDateTime, String zoneId) {
  Objects.requireNonNull(localDateTime, "localDateTime");
  Objects.requireNonNull(zoneId, "zoneId");
  return localDateTime.atZone(ZoneId.of(zoneId));
}

/**
     * LocalDate转ZonedDateTime,时区为系统默认时区
     * @param localDate
     * @return such as 2020-02-19T00:00+08:00[Asia/Shanghai]
     */
    public static ZonedDateTime toZonedDateTime(LocalDate localDate) {
        Objects.requireNonNull(localDate, "localDate");
        return localDate.atStartOfDay().atZone(ZoneId.systemDefault());
    }
    
    /**
     * LocalTime转ZonedDateTime
     * 以当天的日期+LocalTime组成新的ZonedDateTime,时区为系统默认时区
     * @param localTime
     * @return
     */
    public static ZonedDateTime toZonedDateTime(LocalTime localTime) {
        Objects.requireNonNull(localTime, "localTime");
        return LocalDate.now().atTime(localTime).atZone(ZoneId.systemDefault());
    }

    /**
     * Instant转ZonedDateTime,时区为系统默认时区
     * @param instant
     * @return
     */
    public static ZonedDateTime toZonedDateTime(Instant instant) {
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).atZone(ZoneId.systemDefault());
    }
    
    /**
     * epochMilli毫秒转ZonedDateTime,时区为系统默认时区
     * @param epochMilli
     * @return
     */
    public static ZonedDateTime toZonedDateTime(long epochMilli) {
        Objects.requireNonNull(epochMilli, "epochMilli");
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneId.systemDefault())
                .atZone(ZoneId.systemDefault());
    }
    
    /**
     * temporal转ZonedDateTime,时区为系统默认时区
     * @param temporal
     * @return
     */
    public static ZonedDateTime toZonedDateTime(TemporalAccessor temporal) {
        return LocalDateTime.from(temporal).atZone(ZoneId.systemDefault());
    }    

}

 测试代码

package com.xkzhangsan.time.test;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.util.Date;

import org.junit.Test;

import com.xkzhangsan.time.converter.DateTimeConverterUtil;

public class ConverterTest {
    
    @Test
    public void dateConverterTest(){
        System.out.println("===================dateConverterTest=====================");
        Date date = new Date();
        System.out.println(DateTimeConverterUtil.toLocalDateTime(date));
        System.out.println(DateTimeConverterUtil.toLocalDate(date));
        System.out.println(DateTimeConverterUtil.toLocalTime(date));
        System.out.println(DateTimeConverterUtil.toInstant(date));
    }
    
    @Test
    public void localDateTimeConverterTest(){
        System.out.println("===================localDateTimeConverterTest=====================");
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);
        System.out.println(DateTimeConverterUtil.toDate(ldt));
        System.out.println(DateTimeConverterUtil.toLocalDate(ldt));
        System.out.println(DateTimeConverterUtil.toLocalTime(ldt));
        System.out.println(DateTimeConverterUtil.toInstant(ldt));
        System.out.println(DateTimeConverterUtil.toZonedDateTime(ldt));
    }
    
    @Test
    public void localDateConverterTest(){
        System.out.println("===================localDateConverterTest=====================");
        LocalDate ld = LocalDate.now();
        System.out.println(ld);
        System.out.println(DateTimeConverterUtil.toDate(ld));
        System.out.println(DateTimeConverterUtil.toLocalDateTime(ld));
        System.out.println(DateTimeConverterUtil.toInstant(ld));
    }
    
    @Test
    public void localTimeConverterTest(){
        System.out.println("===================localTimeConverterTest=====================");
        LocalTime lt = LocalTime.now();
        System.out.println(lt);
        System.out.println(DateTimeConverterUtil.toDate(lt));
        System.out.println(DateTimeConverterUtil.toLocalDateTime(lt));
        System.out.println(DateTimeConverterUtil.toLocalTime(lt));
        System.out.println(DateTimeConverterUtil.toInstant(lt));
    }    

    
    @Test
    public void instantConverterTest(){
        System.out.println("===================instantConverterTest=====================");
        Instant instant = Instant.now();
        System.out.println(instant);
        System.out.println(DateTimeConverterUtil.toDate(instant));
        System.out.println(DateTimeConverterUtil.toLocalDateTime(instant));
        System.out.println(DateTimeConverterUtil.toLocalDate(instant));
    }
    
    @Test
    public void zonedDateTimeConverterTest(){
        System.out.println("===================zonedDateTimeConverterTest=====================");
        System.out.println("===================ToOther=====================");
        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        System.out.println(zonedDateTime);
        System.out.println(DateTimeConverterUtil.toDate(zonedDateTime));
        System.out.println(DateTimeConverterUtil.toLocalDateTime(zonedDateTime));
        System.out.println(DateTimeConverterUtil.toLocalDate(zonedDateTime));
        System.out.println(DateTimeConverterUtil.toLocalTime(zonedDateTime));
        System.out.println(DateTimeConverterUtil.toInstant(zonedDateTime));
        System.out.println("===================toZonedDateTime=====================");
        System.out.println(zonedDateTime);
        System.out.println(DateTimeConverterUtil.toZonedDateTime(new Date()));
        System.out.println(DateTimeConverterUtil.toZonedDateTime(LocalDateTime.now()));
        System.out.println(DateTimeConverterUtil.toZonedDateTime(LocalDate.now()));
        System.out.println(DateTimeConverterUtil.toZonedDateTime(LocalTime.now()));
        System.out.println(DateTimeConverterUtil.toZonedDateTime(Instant.now()));
    }    
}

输出:

===================dateConverterTest=====================
2020-02-19T16:10:04.366
2020-02-19
16:10:04.366
2020-02-19T08:10:04.366Z
===================localTimeConverterTest=====================
16:10:04.458
Wed Feb 19 16:10:04 CST 2020
2020-02-19T16:10:04.458
16:10:04.458
2020-02-19T08:10:04.458Z
===================localDateConverterTest=====================
2020-02-19
Wed Feb 19 00:00:00 CST 2020
2020-02-19T00:00
2020-02-18T16:00:00Z
===================zonedDateTimeConverterTest=====================
===================ToOther=====================
2020-02-19T16:10:04.464+08:00[Asia/Shanghai]
Wed Feb 19 16:10:04 CST 2020
2020-02-19T16:10:04.464
2020-02-19
16:10:04.464
2020-02-19T08:10:04.464Z
===================toZonedDateTime=====================
2020-02-19T16:10:04.464+08:00[Asia/Shanghai]
2020-02-19T16:10:04.465+08:00[Asia/Shanghai]
2020-02-19T16:10:04.465+08:00[Asia/Shanghai]
2020-02-19T00:00+08:00[Asia/Shanghai]
2020-02-19T16:10:04.466+08:00[Asia/Shanghai]
2020-02-19T16:10:04.466+08:00[Asia/Shanghai]
===================localDateTimeConverterTest=====================
2020-02-19T16:10:04.466
Wed Feb 19 16:10:04 CST 2020
2020-02-19
16:10:04.466
2020-02-19T08:10:04.466Z
2020-02-19T16:10:04.466+08:00[Asia/Shanghai]
===================instantConverterTest=====================
2020-02-19T08:10:04.467Z
Wed Feb 19 16:10:04 CST 2020
2020-02-19T16:10:04.467
2020-02-19

源码地址:https://github.com/xkzhangsan/xk-time

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
2年前
java8时间工具类Localdate、LocaldateTime
优点:1.方便。Date只能是日期加时间的格式,而LocalDate、LocalTime、LocalDateTime 分别代表日期,时间,日期时间,非常灵活。再就是后者在日期计算及格式化方面非常简单易用,而Date要繁琐很多。2.线程安全。传统时间类不支持多线程安全。缺点<目前发现的坑:1.在比较日期相隔
Wesley13 Wesley13
2年前
java常用类(2)
三、时间处理相关类Date类:计算机世界把1970年1月1号定为基准时间,每个度量单位是毫秒(1秒的千分之一),用long类型的变量表示时间。Date分配Date对象并初始化对象,以表示自从标准基准时间(称为“历元”(epoch),即1970年1月1日08:00:00GMT)以来的指定毫秒数。示例:packagecn.tanjian
Wesley13 Wesley13
2年前
java8 时间类与Date类的相互转化
java8时间类与Date类的相互转化在转换中,我们需要注意,因为java8之前Date是包含日期和时间的,而LocalDate只包含日期,LocalTime只包含时间,所以与Date在互转中,势必会丢失日期或者时间,或者会使用起始时间。如果转LocalDateTime,那么就不存在信息误差。//Date与Instant的相互转化
Wesley13 Wesley13
2年前
java8的时间和`Date`的对比
java8的时间和Date的对比java8提供了新的时间接口。相对Date,Calendar,个人感觉最大的好处是对时间操作的学习成本很低,比Calendar低。1\.LocalDate,LocalTime,LocalDateTime
皕杰报表(关于日期时间时分秒显示不出来)
在使用皕杰报表设计器时,数据据里面是日期型,但当你web预览时候,发现有日期时间类型的数据时分秒显示不出来,只有年月日能显示出来,时分秒显示为0:00:00。1.可以使用tochar解决,数据集用selecttochar(flowdate,"yyyyMMddHH:mm:ss")fromtablename2.也可以把数据库日期类型date改成timestamp
Wesley13 Wesley13
2年前
Java日期时间API系列20
  Java日期时间API系列19Jdk8中java.time包中的新的日期时间API类,ZonedDateTime与ZoneId和LocalDateTime的关系,ZonedDateTime格式化和时区转换等。(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fwww.cnbl
Wesley13 Wesley13
2年前
Java日期时间API系列31
  时间戳是指格林威治时间1970年01月01日00时00分00秒起至现在的总毫秒数,是所有时间的基础,其他时间可以通过时间戳转换得到。Java中本来已经有相关获取时间戳的方法,Java8后增加新的类Instant等专用于处理时间戳问题。 1获取时间戳的方法和性能对比1.1获取时间戳方法Java8以前
Stella981 Stella981
2年前
HIVE 时间操作函数
日期函数UNIX时间戳转日期函数: from\_unixtime语法:   from\_unixtime(bigint unixtime\, string format\)返回值: string说明: 转化UNIX时间戳(从19700101 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式举例:hive   selec
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这