最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java8中LocalDateTime與時(shí)間戳timestamp的互相轉(zhuǎn)換

 更新時(shí)間:2021年03月05日 09:15:33   作者:荒野大碼農(nóng)  
這篇文章主要給大家介紹了關(guān)于Java8中LocalDateTime與時(shí)間戳timestamp的互相轉(zhuǎn)換,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

Java8 LocalDateTime與timestamp轉(zhuǎn)換

將timestamp轉(zhuǎn)為L(zhǎng)ocalDateTime

public LocalDateTime timestamToDatetime(long timestamp){
  Instant instant = Instant.ofEpochMilli(timestamp);
  return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
 }

將LocalDataTime轉(zhuǎn)為timestamp

public long datatimeToTimestamp(LocalDateTime ldt){
  long timestamp = ldt.toInstant(ZoneOffset.of("+8")).toEpochMilli();
  return timestamp;
 }

我在網(wǎng)上還找到了另一個(gè)將datetime轉(zhuǎn)為時(shí)間戳的方法:

ZoneId zone = ZoneId.systemDefault();
long timestamp = ldt.atZone(zone).toInstant().toEpochMilli();

Java8的時(shí)間轉(zhuǎn)為時(shí)間戳的大概的思路就是LocalDateTime先轉(zhuǎn)為Instant,設(shè)置時(shí)區(qū),然后轉(zhuǎn)timestamp。

附一個(gè)Java8中的LocalDateTime工具類

工具類

package com.kingboy.common.utils.date;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.Date;

/*
 * @author kingboy
 * @Date 2017/7/22 下午2:12
 * @Description LocalDateTimeUtils is used to Java8中的時(shí)間類
 */
public class LocalDateTimeUtils {

  //獲取當(dāng)前時(shí)間的LocalDateTime對(duì)象
  //LocalDateTime.now();

  //根據(jù)年月日構(gòu)建LocalDateTime
  //LocalDateTime.of();

  //比較日期先后
  //LocalDateTime.now().isBefore(),
  //LocalDateTime.now().isAfter(),

  //Date轉(zhuǎn)換為L(zhǎng)ocalDateTime
  public static LocalDateTime convertDateToLDT(Date date) {
    return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
  }

  //LocalDateTime轉(zhuǎn)換為Date
  public static Date convertLDTToDate(LocalDateTime time) {
    return Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
  }


  //獲取指定日期的毫秒
  public static Long getMilliByTime(LocalDateTime time) {
    return time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
  }

  //獲取指定日期的秒
  public static Long getSecondsByTime(LocalDateTime time) {
    return time.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
  }

  //獲取指定時(shí)間的指定格式
  public static String formatTime(LocalDateTime time,String pattern) {
    return time.format(DateTimeFormatter.ofPattern(pattern));
  }

  //獲取當(dāng)前時(shí)間的指定格式
  public static String formatNow(String pattern) {
    return formatTime(LocalDateTime.now(), pattern);
  }

  //日期加上一個(gè)數(shù),根據(jù)field不同加不同值,field為ChronoUnit.*
  public static LocalDateTime plus(LocalDateTime time, long number, TemporalUnit field) {
    return time.plus(number, field);
  }

  //日期減去一個(gè)數(shù),根據(jù)field不同減不同值,field參數(shù)為ChronoUnit.*
  public static LocalDateTime minu(LocalDateTime time, long number, TemporalUnit field){
    return time.minus(number,field);
  }

  /**
   * 獲取兩個(gè)日期的差 field參數(shù)為ChronoUnit.*
   * @param startTime
   * @param endTime
   * @param field 單位(年月日時(shí)分秒)
   * @return
   */
  public static long betweenTwoTime(LocalDateTime startTime, LocalDateTime endTime, ChronoUnit field) {
    Period period = Period.between(LocalDate.from(startTime), LocalDate.from(endTime));
    if (field == ChronoUnit.YEARS) return period.getYears();
    if (field == ChronoUnit.MONTHS) return period.getYears() * 12 + period.getMonths();
    return field.between(startTime, endTime);
  }

  //獲取一天的開(kāi)始時(shí)間,2017,7,22 00:00
  public static LocalDateTime getDayStart(LocalDateTime time) {
    return time.withHour(0)
        .withMinute(0)
        .withSecond(0)
        .withNano(0);
  }

  //獲取一天的結(jié)束時(shí)間,2017,7,22 23:59:59.999999999
  public static LocalDateTime getDayEnd(LocalDateTime time) {
    return time.withHour(23)
        .withMinute(59)
        .withSecond(59)
        .withNano(999999999);
  }

}

測(cè)試類

package com.kingboy.common.localdatetimeutils;

import com.kingboy.common.utils.date.LocalDateTimeUtils;
import org.junit.Test;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

import static com.kingboy.common.utils.date.LocalDateTimeUtils.getDayEnd;
import static com.kingboy.common.utils.date.LocalDateTimeUtils.getDayStart;

/**
 * @author kingboy
 * @Date 2017/7/22 下午7:16
 * @Description LocaDateTimeUtilsTest is used to 測(cè)試LocalDateTime工具
 */
public class LocaDateTimeUtilsTest {

  @Test
  public void format_test() {
    System.out.println(LocalDateTimeUtils.formatNow("yyyy年MM月dd日 HH:mm:ss"));
  }

  @Test
  public void betweenTwoTime_test() {
    LocalDateTime start = LocalDateTime.of(1993, 10, 13, 11, 11);
    LocalDateTime end = LocalDateTime.of(1994, 11, 13, 13, 13);
    System.out.println("年:" + LocalDateTimeUtils.betweenTwoTime(start, end, ChronoUnit.YEARS));
    System.out.println("月:" + LocalDateTimeUtils.betweenTwoTime(start, end, ChronoUnit.MONTHS));
    System.out.println("日:" + LocalDateTimeUtils.betweenTwoTime(start, end, ChronoUnit.DAYS));
    System.out.println("半日:" + LocalDateTimeUtils.betweenTwoTime(start, end, ChronoUnit.HALF_DAYS));
    System.out.println("小時(shí):" + LocalDateTimeUtils.betweenTwoTime(start, end, ChronoUnit.HOURS));
    System.out.println("分鐘:" + LocalDateTimeUtils.betweenTwoTime(start, end, ChronoUnit.MINUTES));
    System.out.println("秒:" + LocalDateTimeUtils.betweenTwoTime(start, end, ChronoUnit.SECONDS));
    System.out.println("毫秒:" + LocalDateTimeUtils.betweenTwoTime(start, end, ChronoUnit.MILLIS));
    //=============================================================================================
    /*
                   年:1
                   月:13
                   日:396
                   半日:792
                   小時(shí):9506
                   分鐘:570362
                   秒:34221720
                   毫秒:34221720000
    */
  }

  @Test
  public void plus_test() {
    //增加二十分鐘
    System.out.println(LocalDateTimeUtils.formatTime(LocalDateTimeUtils.plus(LocalDateTime.now(),
        20,
        ChronoUnit.MINUTES), "yyyy年MM月dd日 HH:mm"));
    //增加兩年
    System.out.println(LocalDateTimeUtils.formatTime(LocalDateTimeUtils.plus(LocalDateTime.now(),
        2,
        ChronoUnit.YEARS), "yyyy年MM月dd日 HH:mm"));
    //=============================================================================================
    /*
                    2017年07月22日 22:53
                    2019年07月22日 22:33
     */
  }

  @Test
  public void dayStart_test() {
    System.out.println(getDayStart(LocalDateTime.now()));
    System.out.println(getDayEnd(LocalDateTime.now()));
    //=============================================================================================
    /*
                    2017-07-22T00:00
                2017-07-22T23:59:59.999999999
     */
  }

}

總結(jié)

到此這篇關(guān)于Java8中LocalDateTime與時(shí)間戳timestamp互相轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)Java8 LocalDateTime與timestamp轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring cloud Eureka注冊(cè)中心搭建的方法

    Spring cloud Eureka注冊(cè)中心搭建的方法

    這篇文章主要介紹了Spring cloud Eureka注冊(cè)中心搭建的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • 帶你輕松了解Modbus協(xié)議

    帶你輕松了解Modbus協(xié)議

    這篇文章主要給大家介紹了關(guān)于Modbus協(xié)議的相關(guān)資料,此協(xié)議定義了一個(gè)控制器能認(rèn)識(shí)使用的消息結(jié)構(gòu),而不管它們是經(jīng)過(guò)何種網(wǎng)絡(luò)進(jìn)行通信的,需要的朋友可以參考下
    2021-11-11
  • Mapstruct對(duì)象插入數(shù)據(jù)庫(kù)某個(gè)字段總是為空的bug詳解

    Mapstruct對(duì)象插入數(shù)據(jù)庫(kù)某個(gè)字段總是為空的bug詳解

    這篇文章主要為大家介紹了在一次需求開(kāi)發(fā)Mapstruct中對(duì)象插入數(shù)據(jù)庫(kù)某個(gè)字段總是為空的bug問(wèn)題詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • 使用res:bean屬性復(fù)制避免null值覆蓋版本

    使用res:bean屬性復(fù)制避免null值覆蓋版本

    這篇文章主要介紹了使用res:bean屬性復(fù)制避免null值覆蓋版本的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 基于IntBuffer類的基本用法(詳解)

    基于IntBuffer類的基本用法(詳解)

    下面小編就為大家?guī)?lái)一篇基于IntBuffer類的基本用法(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-05-05
  • 如何通過(guò)自定義spring?invalidator注解校驗(yàn)數(shù)據(jù)合法性

    如何通過(guò)自定義spring?invalidator注解校驗(yàn)數(shù)據(jù)合法性

    這篇文章主要介紹了如何通過(guò)自定義spring?invalidator注解校驗(yàn)數(shù)據(jù)合法性,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • java 排序算法之快速排序

    java 排序算法之快速排序

    這篇文章主要介紹了java 排序算法之快速排序,文中通過(guò)圖片和代碼講解相關(guān)知識(shí)非常詳細(xì),大家如果有需要的話可以參考一下這篇文章
    2021-09-09
  • Springboot項(xiàng)目如何獲取所有的接口

    Springboot項(xiàng)目如何獲取所有的接口

    這篇文章主要介紹了Springboot項(xiàng)目如何獲取所有的接口,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java中ArrayList和SubList的坑面試題

    Java中ArrayList和SubList的坑面試題

    集合是Java開(kāi)發(fā)日常開(kāi)發(fā)中經(jīng)常會(huì)使用到的,下面這篇文章主要給大家介紹了關(guān)于Java中ArrayList和SubList的坑面試題,需要的朋友可以參考下
    2022-05-05
  • SpringBoot使用Sa-Token實(shí)現(xiàn)賬號(hào)封禁、分類封禁、階梯封禁的示例代碼

    SpringBoot使用Sa-Token實(shí)現(xiàn)賬號(hào)封禁、分類封禁、階梯封禁的示例代碼

    本文主要介紹了SpringBoot使用Sa-Token實(shí)現(xiàn)賬號(hào)封禁、分類封禁、階梯封禁的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07

最新評(píng)論

福泉市| 太康县| 壶关县| 祁阳县| 邵阳市| 德化县| 富源县| 林芝县| 合肥市| 阿图什市| 陈巴尔虎旗| 新兴县| 扬州市| 渭南市| 吴堡县| 那坡县| 清丰县| 灵寿县| 虞城县| 平远县| 双桥区| 丹东市| 丰城市| 佛山市| 屯昌县| 蓝山县| 杭锦旗| 资溪县| 兴文县| 宁强县| 土默特左旗| 夏津县| 响水县| 孝义市| 普兰县| 临朐县| 邻水| 克拉玛依市| 凯里市| 怀仁县| 牙克石市|