java如何獲取10位和13位時間戳
java獲取10位和13位時間戳
1.根據(jù)當(dāng)前時間獲取13位時間戳 精度是毫秒(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)前時間獲取10位時間戳 精度是秒(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ù)的時間戳轉(zhuǎn)化為10位數(shù)的時間戳 ,除以1000;
- 10位數(shù)的時間戳轉(zhuǎn)化為13位數(shù)的時間戳 ,乘以1000;
java時間戳轉(zhuǎn)換
最近在看 Bob 大叔的《代碼整潔之道》,為了在實(shí)踐中體會,寫了一小段代碼,功能是進(jìn)行時間戳轉(zhuǎn)換,第一版完成后根據(jù)書中的一些原則重構(gòu)了下,然后再其中加入了日志記錄模塊,使用的是 java 自帶的 java.util.logging, 主要是看下日志怎么記錄,這個過程中也考慮并學(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é)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳談StringUtils3之StringUtils.isEmpty()和StringUtils.isB的區(qū)別
這篇文章主要介紹了詳談StringUtils3之StringUtils.isEmpty()和StringUtils.isB的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
使用MUI框架構(gòu)建App請求http接口實(shí)例代碼
下面小編就為大家分享一篇使用MUI框架構(gòu)建App請求http接口實(shí)例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
java json字符串轉(zhuǎn)JSONObject和JSONArray以及取值的實(shí)例
這篇文章主要介紹了java json字符串轉(zhuǎn)JSONObject和JSONArray以及取值的實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05

