比较日期以获得经过的日,月,年 [英] Compare Dates to get Elapsed Day,Month,Year
问题描述
我是什么试图实现的是
给定两个日期(包含时间)忽略格式
2013/11/11 5pm // datetime in mysql
2013/11/12 5pm //当前datetime在java代码
我如何计算出两个日期之间是否已经过了整整一天?相同的几个月和一年。
是否从新的减去旧的,如果超过24小时,一天过去了。如果超过30天,一个月过去了吗?怎么去呢?
这基本上是检查每日订阅是否每月/每年已过期。
24小时≠1天
夏令时(DST)或其他异常可能会影响您的计算是一种日历天可能不要24小时。在美国,我们的DST调整是一个小时,一天可能有23或25小时,而不是24。
所以要明确你的业务逻辑要求:24小时或1天。
当然这都取决于时区。如果在 UTC (没有时区偏移)中工作,一天总是24小时(忽略闰秒) )
体面日期时间库
与Java捆绑在一起的java.util.Date和.Calendar类是臭名昭着的麻烦。避免他们相反,使用一个体面的日期时间库,其中Java表示:
- Joda-Time
- java.time包(捆绑在Java 8中,灵感来自
Joda-时间/rel =nofollow> Joda-Time ,由JSR 310定义)时间
使用Joda-Time 2.3的一些示例代码。
String startInput =2013-11-11T17:00Z;
String stopInput =2013-11-12T17:00Z;
DateTimeZone timeZone = DateTimeZone.forID(Europe / Paris);
DateTime start = new DateTime(startInput,timeZone);
DateTime stop = new DateTime(stopInput,timeZone);
int daysBetween = Days.daysBetween(start,stop).getDays();
int hoursBetween = Hours.hoursBetween(start,stop).getHours();
转储到控制台...
System.out.println(start:+ start);
System.out.println(stop:+ stop);
System.out.println(daysBetween:+ daysBetween);
System.out.println(hoursBetween:+ hoursBetween);
运行时
开始:2013-11-11T18:00:00.000 + 01:00
停止:2013-11-12T18:00:00.000 + 01:00
daysBetween:1
hoursBetween:24
I dont have anything to start with here since what i know in date comparisons is just to see if a date if lesser, equals or greater.
what i am trying to achieve is,
Given two Dates(with time included) ignore the formatting
2013/11/11 5pm //datetime in mysql 2013/11/12 5pm //current datetime in java code
How can i compute to see if a full day has elapsed between the two dates ? same with months and a year.
Will it be to subtract the old from the new and if it's greater than 24 hours, a day has elapsed. If it's greater than 30days, a month has elapsed and so on ? how to go about it ?
This is basically to check if a daily subscription, monthly/yearly has expired.
解决方案24 Hours ≠ 1 Day
Daylight Saving Time (DST) or other anomalies may affect your calculations is such a way that a calendar "day" may not be 24 hours long. In the United States where our DST adjustment is one hour, a "day" may have 23 or 25 hours instead of 24.
So be clear on what your business logic demands: 24 hours or 1 day.
Of course this all depends on time zone. If working in UTC (no time zone offset), a day is always 24 hours (ignoring leap seconds).
Decent Date-Time Library
The java.util.Date and .Calendar classes bundled with Java are notoriously troublesome. Avoid them. Instead use a decent date-time library, which in Java means either:
- Joda-Time
- java.time package (bundled in Java 8, inspired by Joda-Time, defined by JSR 310)
Joda-Time
Some example code using Joda-Time 2.3.
String startInput = "2013-11-11T17:00Z"; String stopInput = "2013-11-12T17:00Z"; DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" ); DateTime start = new DateTime( startInput, timeZone ); DateTime stop = new DateTime( stopInput, timeZone ); int daysBetween = Days.daysBetween( start, stop ).getDays(); int hoursBetween = Hours.hoursBetween( start, stop ).getHours();
Dump to console…
System.out.println( "start: " + start ); System.out.println( "stop: " + stop ); System.out.println( "daysBetween: " + daysBetween ); System.out.println( "hoursBetween: " + hoursBetween );
When run…
start: 2013-11-11T18:00:00.000+01:00 stop: 2013-11-12T18:00:00.000+01:00 daysBetween: 1 hoursBetween: 24
这篇关于比较日期以获得经过的日,月,年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!