Java 获取 Unix时间戳
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时间戳的更多相关文章
- delphi java 日期 转换 获取Unix时间戳
获取Unix时间戳 http://www.cnblogs.com/findumars/p/4716753.html 最简单准确一句话 Result:=IntToStr( DateTimeToUnix ...
- C#、Java、Javascript获取Unix时间戳
背景: 因为项目需要,需要几种语言联动开发,日期字段设计的数字型 获取Unix时间戳代码: Java System.currentTimeMillis() Javascript new Date(). ...
- java获取当前时间戳的方法
获取当前时间戳 //方法 一 System.currentTimeMillis(); //方法 二 Calendar.getInstance().getTimeInMillis(); //方法 三 n ...
- Delphi中获取Unix时间戳及注意事项(c语言中time()是按格林威治时间计算的,比北京时间多了8小时)
uses DateUtils;DateTimeToUnix(Now) 可以转换到unix时间,但是注意的是,它得到的时间比c语言中time()得到的时间大了8*60*60这是因为Now是当前时区的时间 ...
- Java获取当前时间戳/时间戳转换
时间戳精度有两个概念:1是精确到秒,2是精确到毫秒. 要操作时间戳和时间戳转换为时间一般对应的对象就是Date,而Date各种转换离不开SimpleDateFormat: 如果是要获取时间指定的年月日 ...
- C++ 获取Unix时间戳
什么是Unix时间戳? Unix时间戳(Unix timestamp),或称Unix时间(Unix time).POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970 ...
- Java中将unix时间戳转化为正常显示时间
在unix中时间戳是一串数字表示的,使用起来非常不方便,转化方式如下: //Convert Unix timestamp to normal date style public String Time ...
- Java将Unix时间戳转换成指定格式日期
public String TimeStamp2Date(String timestampString, String formats){ Long timestamp = Long.pars ...
- 如何在不同编程语言中获取现在的Unix时间戳(Unix timestamp)
Java time JavaScript Math.round(new Date().getTime()/1000)getTime()返回数值的单位是毫秒 Microsoft .NET / C# ep ...
随机推荐
- 如何使用Javascript判断浏览器终端设备
WEB开发中如何通过Javascript来判断终端为PC.IOS(iphone).Android呢? 可以通过判断浏览器的userAgent,用正则来判断手机是否是ios和Android客户端 var ...
- python-基础介绍
一.Linux基础 - 计算机以及日后我们开发的程序防止的服务器的简单操作 二.Python开发 http://www.cnblogs.com/wupeiqi/articles/5433893.htm ...
- 项目中可能用到的demo
1. 轮播图 https://github.com/codingZero/XRCarouselView 2. 图表 https://github.com/Zirkfied/ZFChart
- 循序渐进Python3(十一) --6-- Ajax 实现跨域请求 jsonp 和 cors
Ajax操作如何实现跨域请求? Ajax (XMLHttpRequest)请求受到同源策略的限制. Ajax通过XMLHttpRequest能够与远程的服务器进行信息交互,另外 ...
- bat调用bat的一个巨坑
[一个巨坑] a.bat的内容:echo 1b.batecho 2执行结果:运行a.bat时,输出1,然后调用b.bat, 但是 echo 2 显示不出来. bat怎么调用bat文件并返回? 例如主文 ...
- iOS基础-NSString及NSMutableString剖析
一.NSString头文件 NSString : NSObject 实现协议: NSCopying/NSMutableCopying/NSSecureCoding 类别: //扩展类别 NSStrin ...
- char类型输出地址
问题描述: 当输出char的地址时,发现输出的是一个字符: char ch = 'a'; cout<<&ch<<endl;//a @ 因为cout得到一个char类型的 ...
- Linux系统中配置jdk
在Linux系统下安装jdk 1.到Oracle公司的官网里下载好jdk,网址 http://www.oracle.com/technetwork/java/javase/downloads/jdk8 ...
- [转]反向ajax项目
ASP.NET WebSocket & Comet Ajax Library (Reverse Ajax - Server Push) 项目链接:http://pokein.codeple ...
- linux下添加链接与删除链接(ln命令的用法)
添加链接使用ln命令用法:#ln --help用法:ln [选项]... 目标 [链接名]或:ln [选项]... 目标... 目录或:ln [选项]... --target-directory=目录 ...