zoukankan      html  css  js  c++  java
  • Java获取精确到秒的时间戳

    1、时间戳简介:

    时间戳的定义:通常是一个字符序列,唯一地标识某一刻的时间。数字时间戳技术是数字签名技术一种变种的应用。是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数(引用自百度百科)

    2、Java中的时间戳:

    在不同的开发语言中,获取到的时间戳的长度是不同的,例如C++中的时间戳是精确到秒的,但是Java中的时间戳是精确到毫秒的,这样在涉及到不同语言的开发过程中,如果不进行统一则会出现一些时间不准确的问题。

    3、Java中的两种获取精确到秒的时间戳的方法:

    Java中的时间戳的毫秒主要通过最后的三位来进行计量的,我们通过两种不同的方式将最后三位去掉。

    方法一:通过String.substring()方法将最后的三位去掉

    /** 
     * 获取精确到秒的时间戳 
     * @return 
     */  
    public static int getSecondTimestamp(Date date){  
        if (null == date) {  
            return 0;  
        }  
        String timestamp = String.valueOf(date.getTime());  
        int length = timestamp.length();  
        if (length > 3) {  
            return Integer.valueOf(timestamp.substring(0,length-3));  
        } else {  
            return 0;  
        }  
    }  

    方法二:通过整除将最后的三位去掉

    / 
      获取精确到秒的时间戳 
     * @param date 
     * @return 
     /  
    public static int getSecondTimestampTwo(Date date){  
        if (null == date) {  
            return 0;  
        }  
        String timestamp = String.valueOf(date.getTime()/1000);  
        return Integer.valueOf(timestamp);  
    }  
  • 相关阅读:
    js-url打开方式
    eclipse删除所有空行
    Oracle重启 error: ora-01034:oracle not available ora-27101:shared memory realm does not exist
    最近面试遇到了高阶函数的概念混乱了
    关于跨域的cookie问题
    二叉树 呜呜
    函数的尾递归
    react context
    二叉树
    dom3级事件
  • 原文地址:https://www.cnblogs.com/unknows/p/7455944.html
Copyright © 2011-2022 走看看