简体   繁体   English

Joda-时代以来的天数

[英]Joda-Time Number of Days since Epoch

I have a problem whereby the number of days since epoch returned by Joda-Time library changes depending the time of the date I entered. 我有一个问题,即自Joda-Time库返回的纪元以来的天数根据我输入的日期而变化。 If I enter 2012-05-14 22:00:00 and 2012-05-14 02:00:00 I would expect the same result since they are both on the same day. 如果我输入2012-05-14 22:00:00 2012-05-14 02:00:002012-05-14 02:00:00我会得到相同的结果,因为它们都在同一天。 The following is my code. 以下是我的代码。

        try {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = sdf.parse("2013-05-03 07:00:00");
        Date date2 = sdf.parse("2013-05-03 23:30:00");


        MutableDateTime epoch = new MutableDateTime();
        epoch.setDate(0); //Set to Epoch time
        System.out.println("Epoch: " + epoch);
        Days days1 = Days.daysBetween(epoch, new MutableDateTime(date1.getTime()));
        Days days2 = Days.daysBetween(epoch, new MutableDateTime(date2.getTime()));
        System.out.println("1) Days Since Epoch: " + days1.getDays());
        System.out.println("2) Days Since Epoch: " + days2.getDays());
    } catch (ParseException e) {
        e.printStackTrace(); 
    }

Epoch: 1970-01-01T11:09:00.414+01:00
1) Days Since Epoch: 15827
2) Days Since Epoch: 15828

Does anyone have any idea what I'm doing wrong? 有谁知道我做错了什么?

OK found the problem (which was in front of my own eyes :)) ... the epoch I was getting was indeed starting from 1970-01-01 but not from the very first ms of that day. 确定发现了问题(这是在我自己的眼前:) :) ...我得到的时代确实是从1970-01-01开始,但不是从那天的第一个ms开始。

I needed to add the following line to get it sorted: 我需要添加以下行以使其排序:

epoch.setTime(0);

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

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