您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)如何中Java8中將LocalDateTime與時(shí)間戳timestamp進(jìn)行轉(zhuǎn)換,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
Java的特點(diǎn)有哪些 1.Java語(yǔ)言作為靜態(tài)面向?qū)ο缶幊陶Z(yǔ)言的代表,實(shí)現(xiàn)了面向?qū)ο罄碚?,允許程序員以優(yōu)雅的思維方式進(jìn)行復(fù)雜的編程。 2.Java具有簡(jiǎn)單性、面向?qū)ο?、分布式、安全性、平臺(tái)獨(dú)立與可移植性、動(dòng)態(tài)性等特點(diǎn)。 3.使用Java可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序等。
將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。
工具類
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 */ } }
關(guān)于如何中Java8中將LocalDateTime與時(shí)間戳timestamp進(jìn)行轉(zhuǎn)換就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。