unix时间戳是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒。

在大多数的UNIX系统中UNIX时间戳存储为32位,这样会引发2038年问题。

但是,因为需求是需要int类型的UNIX时间戳。 开始的时候我是这样设计的。

/**
* 获取当前事件Unxi 时间戳
* @return
*/
public static int getUnixTimeStamp(){
long rest=System.currentTimeMillis()/1000L;
return (int)rest;
}

从新封装了一些方法。如下稳定性就好很多了。

 package com.xuanyuan.utils;

 public class TimeUtils {

       /**
* Constant that contains the amount of milliseconds in a second
*/
static final long ONE_SECOND = 1000L; /**
* Converts milliseconds to seconds
* @param timeInMillis
* @return The equivalent time in seconds
*/
public static int toSecs(long timeInMillis) {
// Rounding the result to the ceiling, otherwise a
// System.currentTimeInMillis that happens right before a new Element
// instantiation will be seen as 'later' than the actual creation time
return (int)Math.ceil((double)timeInMillis / ONE_SECOND);
} /**
* Converts seconds to milliseconds, with a precision of 1 second
* @param timeInSecs the time in seconds
* @return The equivalent time in milliseconds
*/
public static long toMillis(int timeInSecs) {
return timeInSecs * ONE_SECOND;
} /**
* Converts a long seconds value to an int seconds value and takes into account overflow
* from the downcast by switching to Integer.MAX_VALUE.
* @param seconds Long value
* @return Same int value unless long > Integer.MAX_VALUE in which case MAX_VALUE is returned
*/
public static int convertTimeToInt(long seconds) {
if (seconds > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
} else {
return (int) seconds;
}
} }

Java 获取 Unix时间戳的更多相关文章

  1. delphi java 日期 转换 获取Unix时间戳

    获取Unix时间戳 http://www.cnblogs.com/findumars/p/4716753.html 最简单准确一句话 Result:=IntToStr(  DateTimeToUnix ...

  2. C#、Java、Javascript获取Unix时间戳

    背景: 因为项目需要,需要几种语言联动开发,日期字段设计的数字型 获取Unix时间戳代码: Java System.currentTimeMillis() Javascript new Date(). ...

  3. java获取当前时间戳的方法

    获取当前时间戳 //方法 一 System.currentTimeMillis(); //方法 二 Calendar.getInstance().getTimeInMillis(); //方法 三 n ...

  4. Delphi中获取Unix时间戳及注意事项(c语言中time()是按格林威治时间计算的,比北京时间多了8小时)

    uses DateUtils;DateTimeToUnix(Now) 可以转换到unix时间,但是注意的是,它得到的时间比c语言中time()得到的时间大了8*60*60这是因为Now是当前时区的时间 ...

  5. Java获取当前时间戳/时间戳转换

    时间戳精度有两个概念:1是精确到秒,2是精确到毫秒. 要操作时间戳和时间戳转换为时间一般对应的对象就是Date,而Date各种转换离不开SimpleDateFormat: 如果是要获取时间指定的年月日 ...

  6. C++ 获取Unix时间戳

    什么是Unix时间戳? Unix时间戳(Unix timestamp),或称Unix时间(Unix time).POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970 ...

  7. Java中将unix时间戳转化为正常显示时间

    在unix中时间戳是一串数字表示的,使用起来非常不方便,转化方式如下: //Convert Unix timestamp to normal date style public String Time ...

  8. Java将Unix时间戳转换成指定格式日期

    public String TimeStamp2Date(String timestampString, String formats){     Long timestamp = Long.pars ...

  9. 如何在不同编程语言中获取现在的Unix时间戳(Unix timestamp)

    Java time JavaScript Math.round(new Date().getTime()/1000)getTime()返回数值的单位是毫秒 Microsoft .NET / C# ep ...

随机推荐

  1. 在php中防止SQL注入的方法

    摘要:我们php手手工安装的,php的默认配置文件在 /usr/local/apache2/conf/php.ini,我们最主要就是要配置php.ini中的内容,让我们执行 php能够更安全.整个PH ...

  2. note->notice

    登陆博客园,乍眼一看上一篇博客的日期还是2月底,如今已是5月份,期间好几次想要记录一些东西,总感觉现在一天二十四小时越来越短,有几次登陆博客园甚至连密码都要重置一下才能登陆...这两个月的时间里经理了 ...

  3. mysql优化要点(一)

    一  sql语句中使用函数需要注意,因为有可能结果每条语句都会调用,甚至表内每条记录都会调用. 二  注意转换,会消耗大量性能,甚至字符串的encoding不同,也会出现非常大的消耗 三  myisa ...

  4. C6713的Boot mode

    2014年7月23日,终于把困扰我两个月的问题解决了,甚是嗨皮,所以做下记录,以供后人参考之用. 问题描述:我用的片子是TMS320C6713,通过EMIF总线连接的FLASH,此FLASH分为两部分 ...

  5. UGUI代码分析

    1.canvas,screen模式和world模式区别:screen的绑定摄像机与canvas相对位置不会改变不可改变canvas,world模式下有个event camera相对位置可改变,canv ...

  6. Linux SSH,FTP服务配置

    CentOS-6.4-x86_64-minimal 0.网卡配置 参考:Linux系统\Centos没有网卡eth0配置文件怎么办? - http://jingyan.baidu.com/articl ...

  7. 抽象和封装_JAVA_OOP

    很久没做笔记了,没有以前的刚开始学习软件时候的热情了.包括几年前U盘损坏,数据丢失,通过数据恢复,也只是找回一些零星的碎片. 现在就抽时间把以前的技术笔记找回来,这十条记录在电脑上显示的最后修改日期为 ...

  8. CC1310之使用SMARTRF STUDIO

    SMARTRF STUDIO是TI提供的射频测试软件,在调射频的时候非常非常非常好用,推荐每一个使用TI射频芯片的工程师都要掌握. 1 如何使用? 要使用SMARTRF STUDIO,硬件必须连接仿真 ...

  9. Vue.js 组件重要选项

    (一) new一个Vue对象的时候,可以设置它的属性,其中最重要是三个,分别是 data,,methods,watch. new Vue({ data:{ a:, b:[] }, methods:{ ...

  10. ギリギリ eye (优先队列)

    1.题目: [题目描述] A.D.1999,由坠落地球的"谜之战舰"带来的 Over Technology,揭示了人类历史和远 古文明之间的丝丝联系,促使人类终止彼此间的战争,一方 ...