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. go语言的selector

    For a primary expression x that is not a package name, the selector expression x.f denotes the field ...

  2. python之路-Day5

    1.列表生成式,迭代器&生成器 列表生成式 我现在有个需求,看列表[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],我要求你把列表里的每个值加1. 普通版 a = [0,1,2,3 ...

  3. python之路-Day4

    本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 函数是什么? 函数一词来源于数学,但编程中的「 ...

  4. C#Json序列化数据库对象

    //该方法使用NewtonJson.dll插件,需要引用 //using Newtonsoft.Json;//using Newtonsoft.Json.Converters; var data = ...

  5. Unity Application 前后台切换调用关系

    [Unity Application 前后台切换调用关系] http://blog.csdn.net/aa4790139/article/details/48087877

  6. SQL语言

    SQL语言的分类:DDL DML DQL DCL SQL中的操作无非就是(增删改查) DDL:Data Query Language,数据查询语言! 主要是用来定义和维护数据库的各种操作对象,比如库. ...

  7. ROC曲线、AUC、Precision、Recall、F-measure理解及Python实现

    本文首先从整体上介绍ROC曲线.AUC.Precision.Recall以及F-measure,然后介绍上述这些评价指标的有趣特性,最后给出ROC曲线的一个Python实现示例. 一.ROC曲线.AU ...

  8. TCP数据包的封包和拆包

    //该段博文为引用,非原创. 封包和拆包 作者:fengge8ylf  博客:http://blog.csdn.net/fengge8ylf 对于基于TCP开发的通讯程序,有个很重要的问题需要解决,就 ...

  9. svn:ignore eclipse开发一般忽略文件

    target.project.classpath.settings

  10. [原创]迈出NIOS的第一步,HelloNIOS

    Altera官方推出NIOS已经很久了,个人感觉C+V代码配合会是后面FPGA使用的一个主流,由C来完成一些对时序要求不高,对功能要求偏高的部分,比如运动控制等:由V来配合时序完成高时序要求的需求以及 ...