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. CMD打包文件,解压文件

    压缩%ProgramFiles%\Winrar\rar a c:\123.rar c:\123解压%ProgramFiles%Winrar\unrar.exe x c:\sp\sp.rar c:\sp

  2. Sql Server中不常用的表运算符之PIVOT

    PIVOT是SQL Server2005新添加的一个表运算符,作用在于将行转为列. 先来看看他的基本语法: 来自http://technet.microsoft.com/zh-cn/library/m ...

  3. ArcGIS Engine 刷新问题

    link: http://www.cnblogs.com/Jingkunliu/archive/2013/01/10/2854710.html PartialRefresh方法是部分刷新,效率方面比单 ...

  4. logistic公式形式的由来,从广义线性回归说起

    普通线性回归的形式为:(之所以这么写是因为的线性才是线性的所指) 线性回归模型有一下以下几个特征: 1. 2.x,y 通常取值连续 3.y的分布为正态分布或接近正态. 广义线性模型进行了如下推广: 1 ...

  5. Percona 5.7安装

    一.从官网下载Percona5.7 地址:https://www.percona.com/downloads/Percona-Server-5.7/LATEST/ 需要注意是服务器的版本.我这里选择的 ...

  6. dma驱动

    http://www.crifan.com/files/doc/docbook/dma_pl08x_analysis/release/html/dma_pl08x_analysis.html#idp2 ...

  7. ORACLE临时表空间

    ORACLE临时表空间总结 2014-10-05 11:35 by 潇湘隐者, 临时表空间概念 临 时表空间用来管理数据库排序操作以及用于存储临时表.中间排序结果等临时对象,当ORACLE里需要用到S ...

  8. Tcc学习笔记(三) 使用举例

    TCC的使用以使用第三方库为例子,例子包括:OpenGL , GMP以及SDL等. 1.TCC使用GLUT 去OpenGL下载http://www.opengl.org/resources/libra ...

  9. 【Python全栈笔记】08 [模块二] 20 Oct 递归 -*** 待补充

    递归 引入 递归的表现形式 下面是四个函数,互相调用返回结果 # 引入 递归的表现形式 def f1(): ' def f2(): r = f1() return r def f3(): r = f2 ...

  10. asp.net 琐记

    Page的AutoEventWireup作用是是否引发PreInit Load PreRender Unload几个页面处理流程事件 和控件的事件处理函数无关