• 时间戳


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

    /-----------------Java时间戳

    Java

    new Date().getTime()  获取的时间戳为13位,包含毫秒数

    时间戳10位的  不包含毫秒数

    Java8已经提供了更优化的方式获取时间戳

  • 相关阅读:
    C#线程同步(1)- 临界区&Lock
    详细解析Java中抽象类和接口的区别
    防止重复提交的几种办法
    网页中实现JSON的编辑与显示
    xcode5 ios7升级后的一系列问题解决
    hadoop-2.0.0-mr1-cdh4.2.0源码编译总结
    hadoop-2.0.0-cdh4.2.1源码编译总结
    cocos2d-iphone加入芒果广告
    hadoop2.0 eclipse 源码编译
    HBase学习笔记
  • 原文地址:https://www.cnblogs.com/Dhouse/p/7905361.html
Copyright © 2020-2023  润新知