以给定的日期和星期几的间隔返回日期列表作为布尔值 [英] Return a list of dates given an interval of dates and days of the week as booleans
问题描述
我正在尝试从这些参数返回日期列表:
I'm trying to return a list of dates from these parameters :
LocalDate startDate
LocalDate endDate
Boolean monday ... sunday : booleans
例如:
startDate = 01/03/2016
endDate = 10/03/2016 (included)
Monday = true;
Tuesday = false;
Wednesday = false;
Thursday = true;
Friday = true;
Saturday = false;
Sunday = false;
List of dates returned : [03/03/2016, 04/03/2016, 07/03/2016, 10/03/2016]
我可以使用任何libray吗?我只设法返回了两个日期之间的日期,但是我也不知道该如何使用日期:(我使用的是Java 6)
Is there any libray i can use ? I've only managed to returned the dates between two dates, but i have no idea how to use the days also : (I'm using java 6)
public List<LocalDate> datesBetween(LocalDate start, LocalDate end) {
List<LocalDate> ret = new ArrayList<LocalDate>();
for (LocalDate date = start; !date.isAfter(end); date = date.plusDays(1)) {
ret.add(date);
}
return ret;
}
推荐答案
避免使用旧的日期时间类
避免与Java的早期版本捆绑在一起的旧日期时间类,例如java.util.Date/.Calendar。它们的设计欠佳,令人困惑且麻烦。
Avoid old date-time classes
Avoid the old date-time classes bundled with the earliest versions of Java, such as java.util.Date/.Calendar. They are poorly designed, confusing, and troublesome.
内置于java.time框架Java 8和更高版本取代了旧的日期时间类。请参见教程。
The java.time framework built into Java 8 and later supplants the old date-time classes. See Tutorial.
用于Java 6& 7,检查 ThreeTen-Backport 项目( ThreeTen是指JSR 310定义java.time )。对于专门针对Android的用户,请参见围绕Backport的Android包装器, ThreeTenABP 。
For use with Java 6 & 7, check out the ThreeTen-Backport project ('ThreeTen' refers to JSR 310 defining java.time). For Android specifically, see the Android wrapper around the Backport, ThreeTenABP.
新类包括 LocalDate
,它是仅包含日期的值,没有日期和时间段。您需要的另一个方便的类是枚举 DayOfWeek
一个>。 使用这些对象而不是字符串来使您的代码类型安全,可自动记录文档并可以本地化。
The new classes include LocalDate
for a date-only value without time-of-day and without time zone. Another handy class you need is the enum DayOfWeek
. Use these objects rather than strings to make your code type-safe, self-documenting, and localization-ready.
提醒:Java提供了特殊的枚举的高性能 Set
和 Map
实现, EnumSet
和 EnumMap
。
Reminder: Java provides special high-performance Set
and Map
implementations for enums, EnumSet
and EnumMap
.
EnumSet<DayOfWeek> dayOfWeekSet = EnumSet.of( DayOfWeek.TUESDAY , DayOfWeek.THURSDAY );
定义日期间隔。
LocalDate start = LocalDate.of( 2016 , Month.MARCH , 1 );
LocalDate stop = start.plusDays( 10 );
定义一个集合以保留我们的预定日期。
Define a collection to keep our targeted dates.
List<LocalDate> localDates = new ArrayList<>();
在该间隔内增加天数。在日期时间工作中,我们通常使用半开放式方法,其中以 inclusive 开始,而以 exclusive 结尾。
Loop the days in that interval. In date-time work we commonly use the Half-Open approach where the beginning in inclusive while the ending is exclusive.
LocalDate localDate = start;
while ( localDate.isBefore( stop ) ) {
if ( dayOfWeekSet.contains ( localDate.getDayOfWeek() ) ) {
localDates.add( localDate );
}
// Prepare for next loop.
localDate = localDate.plusDays( 1 );
}
转储到控制台。
System.out.println( "From: " + start + " to: " + stop + " the dates for days-of-week: " + dayOfWeekSet + " = " + localDates );
从:2016-03-01至:2016-03-11星期几的日期:[星期二,星期四] = [2016-03-01、2016-03-03、2016-03-08、2016-03-10]
From: 2016-03-01 to: 2016-03-11 the dates for days-of-week: [TUESDAY, THURSDAY] = [2016-03-01, 2016-03-03, 2016-03-08, 2016-03-10]
关于 java.time
java.time 框架内置于Java 8及更高版本中。这些类取代了麻烦的旧旧版日期时间类,例如 java.util.Date
, Calendar
,& SimpleDateFormat
。
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
Joda-Time 项目,现在处于维护模式 ,建议迁移到 java.time 类。
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
要了解更多信息,请参见 Oracle教程 。并在Stack Overflow中搜索许多示例和说明。规范为 JSR 310 。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
您可以直接与数据库交换 java.time 对象。使用符合 java.sql。*
类。
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
在哪里获取java.time类?
Where to obtain the java.time classes?
- Java SE 8 , Java SE 9 , Java SE 10 ,以及后来的
- 内置。
- 具有捆绑实现的标准Java API的一部分。
- Java 9添加了一些次要功能和修复。
- Java SE 8, Java SE 9, Java SE 10, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- 许多java.time功能都反向移植到Java 6&
nofollow noreferrer> ThreeTen-Backport 。
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- 较新版本的java.time类的Android捆绑实现。
- 对于较早的Android(< 26), ThreeTenABP 项目采用了 ThreeTen-Backport (如上所述)。请参阅 如何使用ThreeTenABP… 。
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
ThreeTen-Extra 项目使用其他类扩展了java.time。该项目为将来可能在java.time中添加内容打下了基础。您可能会在这里找到一些有用的类,例如
时间间隔
,YearWeek
,YearQuarter
和更多。这篇关于以给定的日期和星期几的间隔返回日期列表作为布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!