java如何獲取10位和13位時(shí)間戳
java獲取10位和13位時(shí)間戳
1.根據(jù)當(dāng)前時(shí)間獲取13位時(shí)間戳 精度是毫秒(ms)
public static void main(String[] args) { System.out.println(System.currentTimeMillis()); System.out.println(Calendar.getInstance().getTimeInMillis()); System.out.println(new Date().getTime()); }
運(yùn)行結(jié)果
2.根據(jù)當(dāng)前時(shí)間獲取10位時(shí)間戳 精度是秒(s)
public static void main(String[] args) { System.out.println(System.currentTimeMillis()/1000); System.out.println(Calendar.getInstance().getTimeInMillis()/1000); System.out.println(new Date().getTime()/1000); }
運(yùn)行結(jié)果
- 13位數(shù)的時(shí)間戳轉(zhuǎn)化為10位數(shù)的時(shí)間戳 ,除以1000;
- 10位數(shù)的時(shí)間戳轉(zhuǎn)化為13位數(shù)的時(shí)間戳 ,乘以1000;
java時(shí)間戳轉(zhuǎn)換
最近在看 Bob 大叔的《代碼整潔之道》,為了在實(shí)踐中體會,寫了一小段代碼,功能是進(jìn)行時(shí)間戳轉(zhuǎn)換,第一版完成后根據(jù)書中的一些原則重構(gòu)了下,然后再其中加入了日志記錄模塊,使用的是 java 自帶的 java.util.logging, 主要是看下日志怎么記錄,這個(gè)過程中也考慮并學(xué)習(xí)了日志應(yīng)該記錄什么信息,其中還有很多不完善的地方,完整代碼如下。
// Main.java package trans; import java.util.Scanner; import java.util.logging.Logger; public class Main { public static void main(String[] args){ Timestamp ts = new Timestamp(); ts.initLogger(); Scanner scanner = new Scanner(System.in); final Logger logger = ts.logger; while(true) { logger.info("Start Choice!"); System.out.println("******************************"); System.out.println("Chose from:\n" + "0. Exit\n" + "1. Date transform to timestamp\n" + "2. trans.Timestamp transform to date"); String choice = scanner.nextLine(); switch (choice) { case "1": System.out.print("Enter Date(format 2020-9-26 11:10:24): "); ts.transDate(scanner, logger); break; case "2": System.out.print("Enter trans.Timestamp: "); ts.transStamp(scanner, logger); break; case "0": logger.info("Program Exit!"); return; default: logger.info("Invalid choice, choose again!"); break; } System.out.print("\n\n"); } } }
// Timestamp.java package trans; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; public class Timestamp { String date; long timestamp; Logger logger; void initLogger(){ MyLogger logger = new MyLogger("TransDateTimestamp"); logger.setLoggerLevel(Level.INFO); logger.setFileHandler("./Trans" + "DateTimstamp.log"); this.logger = logger.logger; } void transDate(Scanner scanner, Logger logger){ try{ this.date = scanner.nextLine(); this.timestamp = this.date2stamp(this.date); logger.info("Trans successfully!"); System.out.print("\033[34;1m" + "Date: " + this.date + "\ntrans.Timestamp: " + this.timestamp + "\033[0m"); } catch (ParseException e) { logger.warning("Input Error, Date Format incorrect!"); } } long date2stamp(String date) throws ParseException { SimpleDateFormat dateFormat = new SimpleDateFormat(); dateFormat.applyPattern("yyyy-MM-dd HH:mm:ss"); Date dateTime = dateFormat.parse(date); return dateTime.getTime(); } void transStamp(Scanner scanner, Logger logger){ try { this.timestamp = Long.parseLong(scanner.nextLine()); this.date = this.stamp2date(this.timestamp); logger.info("Trans successfully!"); System.out.print("\033[34;1m" + "timestamp: " + this.timestamp +"\ndate: " + this.date + "\033[0m"); }catch(NumberFormatException e){ logger.warning("Input Error! Timestamp format incorrect!"); } } String stamp2date(long timestamp){ SimpleDateFormat dateFormat = new SimpleDateFormat(); dateFormat.applyPattern("yyyy-MM-dd HH:mm:ss"); Date datetime = new Date(timestamp); return dateFormat.format(datetime); } }
// MyLogger.java package trans; import java.io.IOException; import java.util.logging.FileHandler; import java.util.logging.Logger; import java.util.logging.Level; public class MyLogger { Logger logger; MyLogger(String loggerName){ logger = Logger.getLogger(loggerName); } public void setLoggerLevel(Level level){ logger.setLevel(level); } public void setFileHandler(String filePath){ try{ FileHandler fileHandler = new FileHandler(filePath); logger.addHandler(fileHandler); }catch(IOException e){ e.printStackTrace(); } } }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java 獲取當(dāng)前時(shí)間及實(shí)現(xiàn)時(shí)間倒計(jì)時(shí)功能【推薦】
這篇文章主要介紹了Java 獲取當(dāng)前時(shí)間及實(shí)現(xiàn)時(shí)間倒計(jì)時(shí)功能 ,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05springboot新建項(xiàng)目jdk只有17/21,無法選中1.8解決辦法
最近博主也有創(chuàng)建springboot項(xiàng)目,發(fā)現(xiàn)了IntelliJ IDEA在通過Spring Initilizer初始化項(xiàng)目的時(shí)候已經(jīng)沒有java8版本的選項(xiàng)了,這里給大家總結(jié)下,這篇文章主要給大家介紹了springboot新建項(xiàng)目jdk只有17/21,無法選中1.8的解決辦法,需要的朋友可以參考下2023-12-12Java中引用類型之強(qiáng)引用、軟引用、弱引用和虛引用詳解
這篇文章主要介紹了Java中引用類型之強(qiáng)引用、軟引用、弱引用和虛引用的相關(guān)資料,通過實(shí)際代碼示例,展示了如何利用引用隊(duì)列來跟蹤對象的回收狀態(tài),并實(shí)現(xiàn)資源的自動(dòng)清理,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-03-03mybatis foreach遍歷LIST讀到數(shù)據(jù)為null的問題
這篇文章主要介紹了mybatis foreach遍歷LIST讀到數(shù)據(jù)為null的問題,具有很好的參考價(jià)值,希望對大家有所幫助。2022-02-02Java中IO流文件讀取、寫入和復(fù)制的實(shí)例
下面小編就為大家?guī)硪黄狫ava中IO流文件讀取、寫入和復(fù)制的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10Java編程實(shí)現(xiàn)五子棋人人對戰(zhàn)代碼示例
這篇文章主要介紹了Java編程實(shí)現(xiàn)五子棋人人對戰(zhàn)代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-11-11java按照模板導(dǎo)出pdf或word文件詳細(xì)代碼
有時(shí)候業(yè)務(wù)中我們需要使用pdf模板生成一份pdf文件,下面這篇文章主要給大家介紹了關(guān)于java按照模板導(dǎo)出pdf或word文件的相關(guān)資料,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下2023-11-11