日期范围的日期范围 [英] Date range in date range

查看:106
本文介绍了日期范围的日期范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,这个任务对我来说似乎很容易,但是我有点困惑,并且会感激一些提示:D



我有一些事件开始和结束时间 - 我想创建一个表与日历周。



因此,我写了一个方法来检查一个事件是否在本周内对它进行着色像这样:



  private boolean inWeek(Date date,Entry pe){
return((pe.getStartsAt())after(Util。 firstDayOfWeek(date))|| pe.getStartsAt()。equals(Util.firstDayOfWeek(date)))
&&(pe.getEndsAt()。before(Util.lastDayOfWeek(date))|| pe .getEndsAt()。equals(Util.lastDayOfWeek(date))));
}

如果事件只持续一周,这种情况是可以的。但是如果事件在本周之前开始,或者在本周之后结束甚至持续了几个星期呢?



它变得非常复杂,我目前的解决方案是这样的:

  private boolean inWeek(Date date,Entry pe){

return(pe.getStartsAt()。after( Util.firstDayOfWeek(date))& pe.getEndsAt()。after(Util.firstDayOfWeek(date))& pe.getEndsAt()之前(Util.lastDayOfWeek(date)))
|| (在Util.lastDayOfWeek(date))& pe.getStartsAt()之前(Util.firstDayOfWeek(date))& pe.getEndsAt()。(Util.lastDayOfWeek (日期)))
|| (pe.getStartsAt()。after(Util.firstDayOfWeek(date))& pe.getEndsAt()之前(Util.lastDayOfWeek(date)))
|| (()())()()()()();()

}

但在某些单元格中仍然没有显示正确的着色。有人对我有任何提示吗?



(...没有提出joda时间^^)

解决方案

Joda-Time



的默认时区。因此,当部署到设置到其他时区的其他机器时,此应用程序会获得不同的结果。使用正确的时区名称,不要使用3个字母的代码。



如果您的应用适用于跨越时区的人员和地点,您应该考虑将目标周定为 UTC / GMT(没有时区偏移)。请注意,StackOverflow是一天天跟踪您的活动。 天由UTC / GMT定义。



这些点是您不应该滚动自己的日期时间逻辑的证据。请改用主管图书馆。在Java中,这意味着 Joda-Time 或新的java.time. *类Java 8(灵感来自Joda-Time)。



ISO Week



顺便说一句, =http://en.wikipedia.org/wiki/ISO_8601#Week_dates =nofollow noreferrer> ISO 8601标准定义了一个周。各国更多的企业和行业正在采用这一标准。遵循该标准可能证明有用。 Joda-Time DateTime实例知道其ISO周数。调用 myDateTime.weekOfWeakYear()。get()



示例代码



  DateTimeZone timeZone = DateTimeZone.forID(Europe / Berlin); 

Interval weekInQuestion = new Interval(new DateTime(2014,1,20,3,4,5,timeZone).withTimeAtStartOfDay(),新的DateTime(2014,1,27,3,4,5 ,timeZone).withTimeAtStartOfDay());

间隔i1 = new Interval(new DateTime(2014,1,2,3,4,5,timeZone),新DateTime(2014,1,3,23,4,5,timeZone)) ;
Interval i2 = new Interval(new DateTime(2014,1,24,3,4,5,timeZone),新的DateTime(2014,1,26,23,59,59,timeZone));
Interval i3 = new Interval(new DateTime(2014,1,6,3,4,5,timeZone),新的DateTime(2014,1,30,3,4,5,timeZone));

boolean i1HitsWeekInQuestion = i1.overlaps(weekInQuestion);
boolean i2HitsWeekInQuestion = i2.overlaps(weekInQuestion);
boolean i3HitsWeekInQuestion = i3.overlaps(weekInQuestion);

转储到控制台...

  System.out.println(weekInQuestion:+ weekInQuestion); 
System.out.println(i1:+ i1 +hits week:+ i1HitsWeekInQuestion);
System.out.println(i2:+ i2 +hits week:+ i2HitsWeekInQuestion);
System.out.println(i3:+ i3 +hits week:+ i3HitsWeekInQuestion);

运行时

  weekInQuestion:2014-01-20T00:00:00.000 + 01:00 / 2014-01-27T00:00:00.000 + 01:00 
i1:2014- 01-02T03:04:05.000 + 01:00 / 2014-01-03T23:04:05.000 + 01:00点击周:false
i2:2014-01-24T03:04:05.000 + 01:00/2014 -01-26T23:59:59.000 + 01:00 hits week:true
i3:2014-01-06T03:04:05.000 + 01:00 / 2014-01-30T03:04:05.000 + 01:00 hit周:true


Actually this task seemed very easy to me, but i got a little bit stuck and would be thankful for some hints :D

I have some events with a start and an end time - and i would like to create a table with calendar weeks.

Therefore i wrote a method to to check if an event is within this week to color it like this:

private boolean inWeek(Date date, Entry pe) {
    return ((pe.getStartsAt().after(Util.firstDayOfWeek(date)) || pe.getStartsAt().equals(Util.firstDayOfWeek(date)))
     && (pe.getEndsAt().before(Util.lastDayOfWeek(date)) || pe.getEndsAt().equals(Util.lastDayOfWeek(date))));
}

This case was okay if events are just lasting one week. but what if the event starts before this week, or ends after this week or even lasts several weeks?

it became very complicated and my current solution was this:

private boolean inWeek(Date date, Entry pe) {

    return  (  pe.getStartsAt().after(Util.firstDayOfWeek(date)) &&  pe.getEndsAt().after(Util.firstDayOfWeek(date)) && pe.getEndsAt().before(Util.lastDayOfWeek(date))    ) 
    ||      (  pe.getStartsAt().before(Util.lastDayOfWeek(date)) &&  pe.getStartsAt().after(Util.firstDayOfWeek(date)) &&  pe.getEndsAt().after(Util.lastDayOfWeek(date))  )
    ||      (  pe.getStartsAt().after(Util.firstDayOfWeek(date)) &&  pe.getEndsAt().before(Util.lastDayOfWeek(date))  )
    ||      (  pe.getStartsAt().before(Util.firstDayOfWeek(date)) && pe.getEndsAt().after(Util.lastDayOfWeek(date)) );

}

but thas still not showing the right coloration in some cells. Does anybody have any hints for me?

(...without proposing joda times ^^)

解决方案

Joda-Time

The Joda-Time 2.3 library makes this work much easier. It includes an Interval class with an overlap method.

See the answer by MadProgrammer for similar code and discussion.

Key Points

The Interval class is smart, considering the beginning of interval to be inclusive and the ending exclusive. You should make comparisons based on the logic of EQUAL TO OR GREATER THAN the start but LESS THAN the stop. Why? Because the moment before the new day is infinitely divisible. You may think, "Well java.util.Date & Joda-Time resolve to milliseconds so I'll use .999". But then you'll be surprised when you port code to Java 8's new java.time.* classes where time resolves to nanoseconds.

To support this comparison, notice that the target week is defined with a call to withTimeAtStartOfDay. Use this method rather than trying to create a midnight by setting zero time elements. This method is smart and handles Daylight Saving Time and other anomalies where there may not be a 00:00:00 midnight time on certain days in certain time zones.

Specify a time zone rather than rely on defaults. All the code in other answers fail to address the time zone. That means they use the default time zone of the JVM. As a consequence, this app gets different results when deployed to other machines set to other time zones. Use proper time zone names, never 3-letter codes.

If your app applies to people and places across time zones, you should consider basing the target week on UTC/GMT (no time zone offset). Notice that is what StackOverflow does in tracking your activity day by day. A "day" is defined by UTC/GMT.

These points are evidence why you should not roll your own date-time logic. Use a competent library instead. In Java, that means either Joda-Time or the new java.time.* classes in Java 8 (inspired by Joda-Time).

ISO Week

By the way, the ISO 8601 standard defines a "week" precisely. More companies and industries in various countries are adopting this standard. Following that standard may prove useful. A Joda-Time DateTime instance knows its ISO week number. Call myDateTime.weekOfWeakYear().get().

Example Code

DateTimeZone timeZone = DateTimeZone.forID( "Europe/Berlin" );

Interval weekInQuestion = new Interval( new DateTime( 2014, 1, 20, 3, 4, 5, timeZone ).withTimeAtStartOfDay(), new DateTime( 2014, 1, 27, 3, 4, 5, timeZone ).withTimeAtStartOfDay() );

Interval i1 = new Interval( new DateTime( 2014, 1, 2, 3, 4, 5, timeZone ), new DateTime( 2014, 1, 3, 23, 4, 5, timeZone ) );
Interval i2 = new Interval( new DateTime( 2014, 1, 24, 3, 4, 5, timeZone ), new DateTime( 2014, 1, 26, 23, 59, 59, timeZone ) );
Interval i3 = new Interval( new DateTime( 2014, 1, 6, 3, 4, 5, timeZone ), new DateTime( 2014, 1, 30, 3, 4, 5, timeZone ) );

boolean i1HitsWeekInQuestion = i1.overlaps( weekInQuestion );
boolean i2HitsWeekInQuestion = i2.overlaps( weekInQuestion );
boolean i3HitsWeekInQuestion = i3.overlaps( weekInQuestion );

Dump to console…

System.out.println( "weekInQuestion: " + weekInQuestion );
System.out.println( "i1: " + i1 + " hits week: " + i1HitsWeekInQuestion );
System.out.println( "i2: " + i2 + " hits week: " + i2HitsWeekInQuestion );
System.out.println( "i3: " + i3 + " hits week: " + i3HitsWeekInQuestion );

When run…

weekInQuestion: 2014-01-20T00:00:00.000+01:00/2014-01-27T00:00:00.000+01:00
i1: 2014-01-02T03:04:05.000+01:00/2014-01-03T23:04:05.000+01:00 hits week: false
i2: 2014-01-24T03:04:05.000+01:00/2014-01-26T23:59:59.000+01:00 hits week: true
i3: 2014-01-06T03:04:05.000+01:00/2014-01-30T03:04:05.000+01:00 hits week: true

这篇关于日期范围的日期范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆