java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > java获取10位和13位时间戳

java如何获取10位和13位时间戳

作者:想养一只!

这篇文章主要介绍了java如何获取10位和13位时间戳问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

java获取10位和13位时间戳

1.根据当前时间获取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());
    }

运行结果


在这里插入图片描述

2.根据当前时间获取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);
    }

运行结果


在这里插入图片描述

java时间戳转换

最近在看 Bob 大叔的《代码整洁之道》,为了在实践中体会,写了一小段代码,功能是进行时间戳转换,第一版完成后根据书中的一些原则重构了下,然后再其中加入了日志记录模块,使用的是 java 自带的 java.util.logging, 主要是看下日志怎么记录,这个过程中也考虑并学习了日志应该记录什么信息,其中还有很多不完善的地方,完整代码如下。

// 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();
        }
    }
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文