Java中的時(shí)間戳各種操作方法詳解
一.何為Java時(shí)間戳
在Java中,時(shí)間戳通常指的是自1970年1月1日午夜(UTC
)以來(lái)的毫秒數(shù)。
這個(gè)概念在Java中主要通過(guò)java.util.Date
類和java.sql.Timestamp
類來(lái)表示
而在Java 8及以后的版本中,引入了新的日期時(shí)間API
,即java.time
包,提供了更多的功能和靈活性。
二.獲取當(dāng)前時(shí)間戳
1.使用System.currentTimeMillis()方法
long currentTimestamp = System.currentTimeMillis();
2. 使用java.util.Date 類
java.util.Date
類可以用來(lái)表示特定的瞬間,它包含了自1970年1月1日午夜(UTC
)以來(lái)的毫秒數(shù)。
// 創(chuàng)建一個(gè)Date對(duì)象表示當(dāng)前時(shí)間 Date now = new Date(); long timestamp = now.getTime(); // 獲取時(shí)間戳 // 從一個(gè)已知的時(shí)間戳創(chuàng)建Date對(duì)象 long timestamp = ... // 假設(shè)你有一個(gè)時(shí)間戳 Date date = new Date(timestamp);
3. 使用java.sql.Timestamp 類
java.sql.Timestamp
類是java.util.Date
的一個(gè)子類,它提供了更高精度的時(shí)間表示,包括納秒
// 創(chuàng)建一個(gè)Timestamp對(duì)象表示當(dāng)前時(shí)間 Timestamp now = new Timestamp(System.currentTimeMillis()); // 從一個(gè)已知的時(shí)間戳創(chuàng)建Timestamp對(duì)象 long timestamp = ... // 假設(shè)你有一個(gè)時(shí)間戳 Timestamp ts = new Timestamp(timestamp);
4.Java 8中的 java.time 包
Java 8引入了一個(gè)新的日期時(shí)間API,提供了更好的處理日期和時(shí)間的類。其中,Instant
類可以用來(lái)表示時(shí)間戳。
// 獲取當(dāng)前時(shí)間的Instant對(duì)象 Instant now = Instant.now(); long timestamp = now.toEpochMilli(); // 獲取時(shí)間戳 // 從一個(gè)已知的時(shí)間戳創(chuàng)建Instant對(duì)象 long timestamp = ... // 假設(shè)你有一個(gè)時(shí)間戳 Instant instant = Instant.ofEpochMilli(timestamp);
三.格式化時(shí)間戳
要將時(shí)間戳轉(zhuǎn)換為可讀的日期時(shí)間格式,可以使用SimpleDateFormat
(Java 8之前)或DateTimeFormatter
(Java 8及以后)。
1.使用 SimpleDateFormat
// 假設(shè)你有一個(gè)時(shí)間戳 long timestamp = ... // 創(chuàng)建SimpleDateFormat對(duì)象 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 將時(shí)間戳轉(zhuǎn)換為Date對(duì)象,然后格式化 String formattedDate = sdf.format(new Date(timestamp)); System.out.println(formattedDate);
2.使用 DateTimeFormatter
// 假設(shè)你有一個(gè)時(shí)間戳 long timestamp = ... // 創(chuàng)建DateTimeFormatter對(duì)象 DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // 將時(shí)間戳轉(zhuǎn)換為Instant對(duì)象,然后轉(zhuǎn)換為L(zhǎng)ocalDateTime對(duì)象,最后格式化 Instant instant = Instant.ofEpochMilli(timestamp); LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); String formattedDate = ldt.format(dtf); System.out.println(formattedDate);
四.時(shí)間戳與字符串的轉(zhuǎn)換
1.將時(shí)間戳轉(zhuǎn)換為字符串
// 假設(shè)你有一個(gè)時(shí)間戳 long timestamp = ... // 使用SimpleDateFormat SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateStr = sdf.format(new Date(timestamp)); // 使用DateTimeFormatter DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); Instant instant = Instant.ofEpochMilli(timestamp); LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); String dateStr = ldt.format(dtf);
2.將字符串轉(zhuǎn)換為時(shí)間戳
// 假設(shè)你有一個(gè)日期時(shí)間的字符串表示 String dateStr = "2023-04-01 12:34:56"; // 使用SimpleDateFormat SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date date = sdf.parse(dateStr); long timestamp = date.getTime(); } catch (ParseException e) { e.printStackTrace(); } // 使用DateTimeFormatter DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime ldt = LocalDateTime.parse(dateStr, dtf); Instant instant = ldt.atZone(ZoneId.systemDefault()).toInstant(); long timestamp = instant.toEpochMilli();
五.時(shí)區(qū)處理
在處理時(shí)間戳?xí)r,時(shí)區(qū)是一個(gè)非常重要的因素。Java中的ZoneId
類可以用來(lái)表示不同的時(shí)區(qū)。
// 獲取當(dāng)前時(shí)區(qū)的偏移量 ZoneId currentZone = ZoneId.systemDefault(); ZonedDateTime zdt = ZonedDateTime.now(currentZone); long offsetInMillis = zdt.getOffset().getTotalSeconds() * 1000;
六.時(shí)間戳的加減
要對(duì)時(shí)間戳進(jìn)行加減操作,可以使用java.time.Duration
類或直接對(duì)毫秒數(shù)進(jìn)行加減。
// 假設(shè)你有一個(gè)時(shí)間戳 long timestamp = ... // 增加一小時(shí)的時(shí)間戳 Instant instant = Instant.ofEpochMilli(timestamp); Duration oneHour = Duration.ofHours(1); Instant newInstant = instant.plus(oneHour); long newTimestamp = newInstant.toEpochMilli(); // 或者直接對(duì)毫秒數(shù)進(jìn)行加減 long newTimestamp = timestamp + (60 * 60 * 1000); // 增加一小時(shí)
總結(jié)
到此這篇關(guān)于Java中時(shí)間戳各種操作方法詳解的文章就介紹到這了,更多相關(guān)Java中時(shí)間戳內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決SpringMVC Controller 接收頁(yè)面?zhèn)鬟f的中文參數(shù)出現(xiàn)亂碼的問(wèn)題
下面小編就為大家分享一篇解決SpringMVC Controller 接收頁(yè)面?zhèn)鬟f的中文參數(shù)出現(xiàn)亂碼的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03Java中replace、replaceAll和replaceFirst函數(shù)的用法小結(jié)
相信會(huì)java的同學(xué)估計(jì)都用過(guò)replace、replaceAll、replaceFirst這三個(gè)函數(shù),可是,我們真的懂他們嗎?下面通過(guò)這篇文章大家再來(lái)好好學(xué)習(xí)學(xué)習(xí)下這幾個(gè)函數(shù)。2016-09-09Java創(chuàng)建數(shù)組、賦值的四種方式詳解(聲明+創(chuàng)建+初始化?)
數(shù)組是一種數(shù)據(jù)結(jié)構(gòu),用來(lái)存儲(chǔ)同一類型值的集合一旦創(chuàng)建了數(shù)組,就不能再改變它的長(zhǎng)度,下面這篇文章主要給大家介紹了關(guān)于Java創(chuàng)建數(shù)組、賦值的四種方式(聲明+創(chuàng)建+初始化?)的相關(guān)資料,需要的朋友可以參考下2024-04-04SpringMVC中的ResourceUrlProviderExposingInterceptor詳解
這篇文章主要介紹了SpringMVC中的ResourceUrlProviderExposingInterceptor詳解,ResourceUrlProviderExposingInterceptor是Spring MVC的一個(gè)HandlerInterceptor,用于向請(qǐng)求添加一個(gè)屬性,需要的朋友可以參考下2023-12-12IntelliJ IDEA(或者JetBrains PyCharm)中彈出"IntelliJ IDEA License
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA(或者JetBrains PyCharm)中彈出"IntelliJ IDEA License Activation"的解決辦法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10Spring中@Transactional注解的屬性說(shuō)明
這篇文章主要介紹了Spring中@Transactional注解的屬性說(shuō)明,@Transactional 是聲明式事務(wù)管理 編程中使用的注解,@Transactional 注解應(yīng)該只被應(yīng)用到 public 方法上,這是由 Spring AOP 的本質(zhì)決定的,需要的朋友可以參考下2023-11-11