繁体   English   中英

使用日历获取今天的 UNIX 时间戳

[英]Getting today's UNIX timestamp with Calendar

我正在尝试使用 Java 日历在 00:00:00 获取今天的 UNIX 时间戳(自 1970 年 1 月 1 日起经过的秒数):

public long getTodayTimestamp() {
    Calendar c = Calendar.getInstance(Locale.getDefault());
    c.setTimeInMillis(System.currentTimeMillis());      
    c.set(  c.get(Calendar.YEAR),
            c.get(Calendar.MONTH),
            c.get(Calendar.DAY_OF_MONTH),
            0, 0, 0
    );
    return c.getTimeInMillis()/1000;
}

问题是它今天(9 月 1 日)返回 1409608800,但是当我尝试使用某些转换器(例如http://www.onlineconversion.com/unix_time.htm )转换它时,我得到了Mon, 01 Sep 2014 22:00:00 GMT ,即我当地时间 9 月 2 日午夜。 基本上,该方法比我需要的时间提前 24 小时返回时间戳。

我哪里搞砸了?

在线转换器返回日期UTC+0
您在时区 +2。
这意味着:您所在时区的 22:00:00 (UTC+0) 是 00:00:00 (UTC+2)
所以,当我没有错(我希望如此)时,一切似乎都是正确的。

编辑:
你的情况的问题:
getTodayTimestamp() 方法返回 UTC+0 中的时间戳
(getCalendar.getTimeInMillis 以 UTC+0 返回)
这意味着您的语言环境 (UTC+2) 中的 00:00:00 是 UTC+0 中的 22:00:00
这就是转换器显示的内容!

看这里:
http://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html#getTimeInMillis()

你可以通过艰难的方式获得unix时间:-)

至少你不必为语言环境等而烦恼

public class GetUnixTime {
    public static void main(String[] args) throws IOException,
            InterruptedException {
        ProcessBuilder ps = new ProcessBuilder("date");
        ps.redirectErrorStream(true);
        Process pr = ps.start();
        StringWriter sw = new StringWriter();
        BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            sw.write(line);
        }
        pr.waitFor();
        in.close();
        sw.flush();
        sw.close();

        System.out.println(">"+sw.toString()+"<"); //parse this guy
    }
}

tl;博士

java.time.Instant   // Represent a moment in UTC.
.now()              // Capture current moment in UTC.
.getEpochSecond()   // Get the whole seconds component within an `Instant`, ignoring any fractional second.

细节

Ben回答似乎是正确的。

避免使用 juCalendar

使用像样的库而不是臭名昭著的 java.util.Date 和 .Calendar 类,日期时间工作要容易得多。 使用Joda-Time或 Java 8 中的新 java.time 包。这两个库都明确而清晰地处理时区。

时间

使用Instant捕获在 UTC 中看到的当前时刻。 根据定义, Instant始终采用 UTC。

Instant instant = Instant.now() ;  // Capture current moment in UTC.

显然,您想要计算自 UTC 中 1970 年第一时刻的纪元引用以来的整秒数。

long secondsSinceEpoch = instant.getEpochSecond() ;  // Get the whole seconds component within an `Instant`, ignoring any fractional second.

乔达时间

Joda-Time 2.4 中的示例。

DateTime nowParis = DateTime.now( DateTimeZone.forID( "Europe/Paris" ) );
DateTime nowUtc = nowParis.withZone( DateTimeZone.UTC );
long millisecondsSinceUnixEpoch = nowParis.getMillis(); // Some result when called on either nowParis or nowUtc.
long secondsSinceUnixEpoch = ( millisecondsSinceUnixEpoch / 1000L );

对这些 DateTime 对象调用toString以验证它们的值。

或者在一行中。

long secondsSinceUnixEpoch = ( DateTime.now().getMillis() / 1000L );

由于我们只需要自当前时刻的纪元以来的秒数,因此时区无关紧要。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM