获得“unixtime”在Java中 [英] Getting "unixtime" in Java

查看:104
本文介绍了获得“unixtime”在Java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Date.getTime()返回自1970年1月1日以来的毫秒数.Unixtime是自1970年1月1日以来的秒数。我通常不用java编写代码,但我正在修复一些错误。我有:

Date.getTime() returns milliseconds since Jan 1, 1970. Unixtime is seconds since Jan 1, 1970. I don't usually code in java, but I'm working on some bug fixes. I have:

Date now = new Date();      
Long longTime = new Long(now.getTime()/1000);
return longTime.intValue();

有没有更好的方法在java中获取unixtime?

Is there a better way to get unixtime in java?

更新

根据John M的建议,我最终得到:

Based on John M's suggestion, I ended up with:

return (int) (System.currentTimeMillis() / 1000L);


推荐答案

避免使用 System.currentTimeMillis()。除以1000可以获得Unix时代。

Avoid the Date object creation w/ System.currentTimeMillis(). A divide by 1000 gets you to Unix epoch.

如评论中所述,您通常需要一个原始的长(小写l长)而不是长的盒装对象(capital -L Long)表示unixTime变量的类型。

As mentioned in a comment, you typically want a primitive long (lower-case-l long) not a boxed object long (capital-L Long) for the unixTime variable's type.

long unixTime = System.currentTimeMillis() / 1000L;

这篇关于获得“unixtime”在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆