简体   繁体   English

在 JAVA 中获取给定日期范围(DateX 和 DateY)之间的所有星期一和星期四日期将是一个很好的实现

[英]What would be a good implementation to get all Monday and Thursday dates Between a given date range (DateX and DateY) in JAVA

I want to get some days between a given range of dates.我想在给定的日期范围内间隔几天。 What would be an optimal solution for this?什么是最佳解决方案? For example, I want to get all Monday, Wednesday and Thursday dates from today up to two months.例如,我想获取从今天到两个月内所有星期一、星期三和星期四的日期。

Using java.time使用 java.time

The modern approach uses the java.time classes.现代方法使用 java.time 类。

Instantiate List objects to collect your results.实例化List对象以收集您的结果。

List<LocalDate> mondays = new ArrayList<>() ;
List<LocalDate> wednesdays = new ArrayList<>() ;
List<LocalDate> thursdays = new ArrayList<>() ;

Determine today's date.确定今天的日期。 This requires a time zone, as the date varies around the globe by zone for any given moment.这需要一个时区,因为在任何给定时刻,全球各地的日期都会因时区而异。

LocalDate today = LocalDate.now( ZoneId.of( "America/Montreal" ) ) ;

Add two more months.再加两个月。 The java.time classes smartly handle the issue of shorter/longer months. java.time 类巧妙地处理了更短/更长月份的问题。 Read the class doc to see if you agree with their behavior/policies.阅读课程文档,看看您是否同意他们的行为/政策。

LocalDate twoMonthsLater = today.plusMonths( 2 );

Loop each date, incrementing one day at a time.循环每个日期,一次递增一天。 Test its day-of-week value using the DayOfWeek enum.使用DayOfWeek枚举测试其星期几值。 Note that you can switch on an Enum object, but oddly cannot include its class name prefix.请注意,您可以switch Enum对象,但奇怪的是不能包含它的类名前缀。 So we use MONDAY rather than my preferred DayOfWeek.MONDAY .所以我们使用MONDAY而不是我首选的DayOfWeek.MONDAY

LocalDate localDate = today ;
while ( localDate.isBefore( twoMonthsLater ) ) {
    DayOfWeek dow = localDate.getDayOfWeek() ;
     switch ( dow ) {
            case MONDAY: 
                mondays.add( localDate ) ;
                break;

            case WEDNESDAY: 
                wednesdays.add( localDate ) ;
                break;

            case THURSDAY:
                thursdays.add( localDate ) ;
                break;

            default: 
                // Ignore any other day-of-week.
                break;
        }
        // Set-up the next loop. Increment by one day at a time.
        localDate = localDate.plusDays( 1 ) ;
}

Dump to console.转储到控制台。

System.out.println( "From " + today + " to " + twoMonthsLater + ":"  );
System.out.println( "mondays: " + mondays ) ;
System.out.println( "wednesdays: " + wednesdays ) ;
System.out.println( "thursdays: " + thursdays ) ;

See this code run live at IdeOne.com .在 IdeOne.com 上查看此代码的实时运行

From 2017-10-17 to 2017-12-17:从 2017-10-17 到 2017-12-17:

mondays: [2017-10-23, 2017-10-30, 2017-11-06, 2017-11-13, 2017-11-20, 2017-11-27, 2017-12-04, 2017-12-11]星期一: [2017-10-23, 2017-10-30, 2017-11-06, 2017-11-13, 2017-11-20, 2017-11-27, 2017-12-04, 2017-12-11 ]

wednesdays: [2017-10-18, 2017-10-25, 2017-11-01, 2017-11-08, 2017-11-15, 2017-11-22, 2017-11-29, 2017-12-06, 2017-12-13]星期三: [2017-10-18, 2017-10-25, 2017-11-01, 2017-11-08, 2017-11-15, 2017-11-22, 2017-11-29, 2017-12-06 , 2017-12-13]

thursdays: [2017-10-19, 2017-10-26, 2017-11-02, 2017-11-09, 2017-11-16, 2017-11-23, 2017-11-30, 2017-12-07, 2017-12-14]星期四: [2017-10-19, 2017-10-26, 2017-11-02, 2017-11-09, 2017-11-16, 2017-11-23, 2017-11-30, 2017-12-07 , 2017-12-14]


About java.time关于java.time

The java.time framework is built into Java 8 and later. java.time框架内置于 Java 8 及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat .这些类取代了麻烦的旧的遗留日期时间类,例如java.util.DateCalendarSimpleDateFormat

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes.现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial .要了解更多信息,请参阅Oracle 教程 And search Stack Overflow for many examples and explanations.并在 Stack Overflow 中搜索许多示例和解释。 Specification is JSR 310 .规范是JSR 310

Where to obtain the java.time classes?在哪里获取 java.time 类?

The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目用附加类扩展了 java.time。 This project is a proving ground for possible future additions to java.time.这个项目是未来可能添加到 java.time 的试验场。 You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more .您可能会在这里找到一些有用的类,例如IntervalYearWeekYearQuarter等等

Calculate the number of weeks between x and y, and take x's weekday into account.计算 x 和 y 之间的周数,并考虑 x 的工作日。

Have a look at java.util.Calendar .看看java.util.Calendar One can get a instance of it, set it to a given date, then, for example, call get(DAY_OF_WEEK) on it.可以获取它的一个实例,将其设置为给定日期,然后,例如,对其调用get(DAY_OF_WEEK) Also it has some nice methods to get number of day in a given year and number of days in a given year.它还有一些很好的方法来获取给定年份的天数和给定年份的天数。

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

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