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