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

查看:24
本文介绍了获取“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?

推荐答案

避免使用 System.currentTimeMillis().除以 1000 让你进入 Unix 时代.

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

如评论中所述,对于 unixTime 变量的类型,您通常需要原始 long(小写-l long)而不是装箱对象 long(大写-L Long).

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天全站免登陆