简体   繁体   English

Java中DateTime对象之间的小数天数

[英]Fractional Number of Days between DateTime objects in Java

How do I find the fractional difference in Days between two Joda-Time DateTime instances? 如何在两个Joda-Time DateTime实例之间找到天数的小数差异?

Days.daysBetween(start, end).getDays() gives me a round number like 10/15 etc. What i need to get is exact value like 10.15 or 15.78 Days.daysBetween(开始,结束).getDays()给我一个像10/15等的数字。我需要得到的是精确值,如10.15或15.78

You might miss minutes due to the round off, if you use Hours.hoursBetween . 如果你使用Hours.hoursBetween,你可能会因为四舍五入而错过几分钟。

Try this : (end.getMillis - start.getMillis() )/(1000*3600*24) 试试这个:(end.getMillis - start.getMillis())/(1000 * 3600 * 24)

Subtract the time in millis and divide by number of millis per day: 以毫克为单位减去时间并除以每天的毫秒数:

double diff = (end.getMillis() - start.getMillis()) / 86400000d;

Test 测试

DateTime start = new DateTime(2015, 1, 1,  0,  0,  0,   0); // Jan 1, 2015
DateTime end   = new DateTime(2015, 7, 4, 14, 23, 55, 876); // Jul 4, 2015 at 2:23:55.876 PM

final int MILLIS_PER_DAY = 24 * 60 * 60 * 1000; // 86_400_000
double diff = (double)(end.getMillis() - start.getMillis()) / MILLIS_PER_DAY;
System.out.println(diff);

int days = (int)diff;
diff = (diff - days) * 24;
int hours = (int)diff;
diff = (diff - hours) * 60;
int minutes = (int)diff;
diff = (diff - minutes) * 60;
int seconds = (int)diff;
diff = (diff - seconds) * 1000;
int millis = (int)Math.round(diff);
System.out.println(days    + " days, "    +
                   hours   + " hours, "   +
                   minutes + " minutes, " +
                   seconds + " seconds, " +
                   millis  + " millis");

Output 产量

184.55828560185185
184 days, 13 hours, 23 minutes, 55 seconds, 876 millis

Note that it's only 13 hours, not 14, because of daylight savings time. 请注意,由于夏令时,它只有13个小时,而不是14个小时。

感谢评论的输入,我使用它精确地工作到秒:

double days = Seconds.secondsBetween(start, end).getSeconds()/86400;

To get a number of days with a fraction, calculate the difference in a small enough unit for your precision requirement, then convert to double before doing the division that converts to days: 要获得分数的天数,请根据精度要求计算足够小的单位差,然后在进行转换为天数的除法之前转换为double

    DateTimeZone zone = DateTimeZone.forID("Europe/Copenhagen");
    DateTime date1 = new DateTime(1997, 1, 1, 0, 0, zone);
    DateTime date2 = new DateTime(1997, 3, 29, 12, 0, zone);

    int seconds = Seconds.secondsBetween(date1, date2).getSeconds();
    double days = (double) seconds / (double) TimeUnit.DAYS.toSeconds(1);
    System.out.println("Difference in days is " + days);

This snippet outputs: 此代码段输出:

Difference in days is 87.5 天数差异为87.5

We need to supply the correct time zone to take transitions to and from summer time (DST) into account. 我们需要提供正确的时区来考虑夏令时(DST)的转换。 In my example both days are in the standard time part of the year in the EU, so 12 hours translate to 0.5 days. 在我的例子中,两天都在欧盟的标准时间部分,因此12小时转换为0.5天。 If for date2 I had picked a date in the beginning of April, after summer time has begun, this would not have been the case. 如果对于date2我在4月初选择了一个日期,在夏季时间开始之后,情况就不是这样了。 For example: 例如:

    DateTime date2 = new DateTime(1997, 4, 1, 12, 0, zone);

Difference in days is 90.45833333333333 天数差异为90.45833333333333

The code will work with a difference of up to about 68 years since an int only holds this many seconds. 代码将使用最多约68年的差异,因为int只持有这么多秒。 If you need a greater span of years, consider using minutes or hours instead of seconds. 如果您需要更长的年限,请考虑使用分钟或小时而不是秒。 If you pass two dates that are too far apart, Joda-Time is friendly enough to throw an exception rather than tacitly giving you an incorrect result. 如果你传递两个相距太远的日期,Joda-Time足够友好,可以抛出异常,而不是默默地给你一个不正确的结果。

This answer was written on the occasion of this duplicate question , and the example datetimes have been taken from there. 这个答案是在这个重复的问题之际写的,并且从那里开始了示例日期时间。

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

相关问题 查找两个 java.time.Instant 对象之间的天数和月数的最佳方法是什么? - What is a best way to find the number of days and months between two java.time.Instant objects? 如何计算两个DateTime对象之间的差异(以天为单位)? - How to calculate the difference (in days) between two DateTime objects? Java中固定位数的科学计数法 - Scientific notation with fixed number of fractional digits in Java 计算特定时区中两个 ZonedDateTime 对象之间的天数? - Calculate number of days between two ZonedDateTime objects in a specific time zone? 两个日期之间的小数月差(Scala 或 Java) - Fractional month difference between 2 dates (Scala or Java) 在 Java 中计算两个 Date 或 Calendar 对象之间的天数 - Working out days between two Date or Calendar objects in Java 用于查找年,月和日中2个日期对象之间差异的Java方法 - Java method to find difference between 2 date objects in years, months and days 如何在java或groovy中查找两个日期之间的天数? - How to find the number of days between two dates in java or groovy? 如何在java中查找两个unix时间戳之间的天数 - How to find number of days between two unix timestamps in java Java 7获得2个日期之间的天数,并正确计算leap年 - Java 7 get number of days between 2 dates with correct calculation for leap years
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM