Java8中LocalDateTime與時間戳timestamp的互相轉(zhuǎn)換
Java8 LocalDateTime與timestamp轉(zhuǎn)換
將timestamp轉(zhuǎn)為LocalDateTime
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)上還找到了另一個將datetime轉(zhuǎn)為時間戳的方法:
ZoneId zone = ZoneId.systemDefault(); long timestamp = ldt.atZone(zone).toInstant().toEpochMilli();
Java8的時間轉(zhuǎn)為時間戳的大概的思路就是LocalDateTime先轉(zhuǎn)為Instant,設(shè)置時區(qū),然后轉(zhuǎn)timestamp。
附一個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中的時間類
*/
public class LocalDateTimeUtils {
//獲取當(dāng)前時間的LocalDateTime對象
//LocalDateTime.now();
//根據(jù)年月日構(gòu)建LocalDateTime
//LocalDateTime.of();
//比較日期先后
//LocalDateTime.now().isBefore(),
//LocalDateTime.now().isAfter(),
//Date轉(zhuǎn)換為LocalDateTime
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();
}
//獲取指定時間的指定格式
public static String formatTime(LocalDateTime time,String pattern) {
return time.format(DateTimeFormatter.ofPattern(pattern));
}
//獲取當(dāng)前時間的指定格式
public static String formatNow(String pattern) {
return formatTime(LocalDateTime.now(), pattern);
}
//日期加上一個數(shù),根據(jù)field不同加不同值,field為ChronoUnit.*
public static LocalDateTime plus(LocalDateTime time, long number, TemporalUnit field) {
return time.plus(number, field);
}
//日期減去一個數(shù),根據(jù)field不同減不同值,field參數(shù)為ChronoUnit.*
public static LocalDateTime minu(LocalDateTime time, long number, TemporalUnit field){
return time.minus(number,field);
}
/**
* 獲取兩個日期的差 field參數(shù)為ChronoUnit.*
* @param startTime
* @param endTime
* @param field 單位(年月日時分秒)
* @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);
}
//獲取一天的開始時間,2017,7,22 00:00
public static LocalDateTime getDayStart(LocalDateTime time) {
return time.withHour(0)
.withMinute(0)
.withSecond(0)
.withNano(0);
}
//獲取一天的結(jié)束時間,2017,7,22 23:59:59.999999999
public static LocalDateTime getDayEnd(LocalDateTime time) {
return time.withHour(23)
.withMinute(59)
.withSecond(59)
.withNano(999999999);
}
}
測試類
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 測試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("小時:" + 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
小時: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與時間戳timestamp互相轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)Java8 LocalDateTime與timestamp轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?Boot解決循環(huán)依賴的過程詳細(xì)記錄
這篇文章主要介紹了Spring?Boot解決循環(huán)依賴的過程,Spring框架通過三級緩存機制解決循環(huán)依賴問題,分別為singletonObjects、earlySingletonObjects和singletonFactories,需要的朋友可以參考下2024-09-09
spring boot 項目利用Jenkins實現(xiàn)自動化部署的教程詳解
這篇文章主要介紹了spring boot 項目利用Jenkins實現(xiàn)自動化部署的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2018-07-07
SpringCloud之微服務(wù)容錯的實現(xiàn)
這篇文章主要介紹了SpringCloud之微服務(wù)容錯的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
Spring?Boot日志SLF4J和Logback示例詳解
這篇文章主要介紹了Spring?Boot日志SLF4J和Logback詳解,Logback相比于Log4j,性能提高了10倍以上的性能,占用的內(nèi)存也變小了,并且文檔十分詳細(xì),推薦使用Slf4j+Logback,需要的朋友可以參考下2023-07-07
IDEA如何使用spring-Initializr快速搭建SpringBoot
這篇文章主要介紹了IDEA如何使用spring-Initializr快速搭建SpringBoot問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05

