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()方法将最后的三位去掉

    1. /** 
    2.  * 获取精确到秒的时间戳 
    3.  * @return 
    4.  */  
    5. public static int getSecondTimestamp(Date date){  
    6.     if (null == date) {  
    7.         return 0;  
    8.     }  
    9.     String timestamp = String.valueOf(date.getTime());  
    10.     int length = timestamp.length();  
    11.     if (length > 3) {  
    12.         return Integer.valueOf(timestamp.substring(0,length-3));  
    13.     } else {  
    14.         return 0;  
    15.     }  
    16. }  

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

    1. /** 
    2.  * 获取精确到秒的时间戳 
    3.  * @param date 
    4.  * @return 
    5.  */  
    6. public static int getSecondTimestampTwo(Date date){  
    7.     if (null == date) {  
    8.         return 0;  
    9.     }  
    10.     String timestamp = String.valueOf(date.getTime()/1000);  
    11.     return Integer.valueOf(timestamp);  
    12. }  


  • 相关阅读:
    Quartz.Net 作业调度后台管理系统,基于Extjs
    [备份]EntityFramework
    WebMisSharp升级说明,最新版本1.6.0
    AllPay(欧付宝)支付接口集成
    Paypal Rest Api自定义物流地址(跳过填写物流地址)
    根据IP获取国家
    ViewBag 找不到编译动态表达式所需的一种或多种类型,是否缺少引用?
    Extjs4 DateTimeField,日期时间控件完美版
    IOS Swift 训练
    .Net集成PayPal的Demo
  • 原文地址:https://www.cnblogs.com/jpfss/p/7132424.html
Copyright © 2011-2022 走看看