英文:
How to find every week dates between startDate and endDate passing string like " Wed, Thu" which is dynamically passed
问题
当我使用字符串"Mon, Tue, Wed, Thu, Fri, Sat, Sun"来传递startDate和endDate,并且还附带了一周中的某些天,我想要在startDate和endDate之间,根据传递的每周天数获得日期。
天数可以是整个星期,也可以是传递给方法的自定义天数。
- 我的尝试代码
public List<LocalDate> getWeeklyDateByStringofDays(String DaysofWeek, LocalDate startDate, LocalDate endDate) {
List<String> daysOfWeekList = Arrays.asList(DaysofWeek.split(","));
// 我不知道该如何做
}
英文:
when i passing startDate and endDate with day of week like as string "Mon ,Tue, Wed, Thu, Fri, Sat, Sun" then i want date of every weekly based on passing day of week between startDate and endDate.
Days is may be fully week or custom day from passing into method.
1.My try code
public List<LocalDate> getWeeklyDateByStringofDays(String DaysofWeek, LocalDate startDate, LocalDate endDate) {
List<String> daysOfWeekList = Arrays.asList(DaysofWeek.split(","));
// How can do it no idea
}
答案1
得分: 3
你可以从LocalDate
中以三个字母的格式(例如Mon
)获取DayOfWeek
,并且可以使用以下方式检查是否包含在daysOfWeek
中:
localdate.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.US))
然后,你可以使用Stream.iterate
迭代日期范围,并且如果LocalDate
的DayOfWeek
包含在你的daysOfWeek
中,则将这些日期收集到列表中:
public List<LocalDate> getWeeklyDateByStringofDays(String daysOfWeek, LocalDate startDate,
LocalDate endDate) {
final int days = (int) startDate.until(endDate, ChronoUnit.DAYS);
return Stream.iterate(startDate, d -> d.plusDays(1))
.limit(days)
.filter(d -> daysOfWeek.contains(d.getDayOfWeek()
.getDisplayName(TextStyle.SHORT, Locale.US)))
.collect(Collectors.toList());
}
英文:
You can get DayOfWeek
from LocalDate
in the format of three letters(e.g.Mon
) and check contains in daysOfWeek
like
localdate.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.US))
And iterate over the range using Stream.iterate
and filter if DayOfWeek
of LocalDate
contains in your daysOfWeek
then collect localdates in list
public List<LocalDate> getWeeklyDateByStringofDays(String daysOfWeek, LocalDate startDate,
LocalDate endDate) {
final int days = (int) startDate.until(endDate, ChronoUnit.DAYS);
return Stream.iterate(startDate, d -> d.plusDays(1))
.limit(days)
.filter(d -> daysOfWeek.contains(d.getDayOfWeek()
.getDisplayName(TextStyle.SHORT, Locale.US)))
.collect(Collectors.toList());
}
答案2
得分: 2
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// Test
System.out.println(getWeeklyDateByStringofDays("Mon, Tue, Wed", LocalDate.parse("2020-09-14"),
LocalDate.parse("2020-12-14")));
}
static List<LocalDate> getWeeklyDateByStringofDays(String daysOfWeek, LocalDate startDate, LocalDate endDate) {
// Split the string on optional whitespace followed by comma which in turn may
// be followed by optional whitespace
String[] daysOfWeekList = daysOfWeek.split("\\s*,\\s*");
// The list to be populated with desired dates and returned
List<LocalDate> result = new ArrayList<>();
// Formatter to get only day name e.g. Mon from the date
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE", Locale.ENGLISH);
for (String day : daysOfWeekList) {
// Loop starting with the startDate until the endDate with a step of one day
for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) {
if (date.format(dtf).equals(day)) {
result.add(date);
}
}
}
// Sort the list
Collections.sort(result);
return result;
}
}
输出:
[2020-09-14, 2020-09-15, 2020-09-16, 2020-09-21, 2020-09-22, 2020-09-23, 2020-09-28, 2020-09-29, 2020-09-30, 2020-10-05, 2020-10-06, 2020-10-07, 2020-10-12, 2020-10-13, 2020-10-14, 2020-10-19, 2020-10-20, 2020-10-21, 2020-10-26, 2020-10-27, 2020-10-28, 2020-11-02, 2020-11-03, 2020-11-04, 2020-11-09, 2020-11-10, 2020-11-11, 2020-11-16, 2020-11-17, 2020-11-18, 2020-11-23, 2020-11-24, 2020-11-25, 2020-11-30, 2020-12-01, 2020-12-02, 2020-12-07, 2020-12-08, 2020-12-09, 2020-12-14]
注意: 如果参数中的星期几名称可以是任何大小写形式,请在上述代码中将 date.format(dtf).equals(day)
替换为 date.format(dtf).equalsIgnoreCase(day)
。
英文:
You can do it as follows:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// Test
System.out.println(getWeeklyDateByStringofDays("Mon, Tue, Wed", LocalDate.parse("2020-09-14"),
LocalDate.parse("2020-12-14")));
}
static List<LocalDate> getWeeklyDateByStringofDays(String daysOfWeek, LocalDate startDate, LocalDate endDate) {
// Split the string on optional whitespace followed by comma which in turn may
// be followed by optional whitespace
String[] daysOfWeekList = daysOfWeek.split("\\s*,\\s*");
// The list to be populated with desired dates and returned
List<LocalDate> result = new ArrayList<>();
// Formatter to get only day name e.g. Mon from the date
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE", Locale.ENGLISH);
for (String day : daysOfWeekList) {
// Loop starting with the startDate until the endDate with a step of one day
for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) {
if (date.format(dtf).equals(day)) {
result.add(date);
}
}
}
// Sort the list
Collections.sort(result);
return result;
}
}
Output:
[2020-09-14, 2020-09-15, 2020-09-16, 2020-09-21, 2020-09-22, 2020-09-23, 2020-09-28, 2020-09-29, 2020-09-30, 2020-10-05, 2020-10-06, 2020-10-07, 2020-10-12, 2020-10-13, 2020-10-14, 2020-10-19, 2020-10-20, 2020-10-21, 2020-10-26, 2020-10-27, 2020-10-28, 2020-11-02, 2020-11-03, 2020-11-04, 2020-11-09, 2020-11-10, 2020-11-11, 2020-11-16, 2020-11-17, 2020-11-18, 2020-11-23, 2020-11-24, 2020-11-25, 2020-11-30, 2020-12-01, 2020-12-02, 2020-12-07, 2020-12-08, 2020-12-09, 2020-12-14]
Note: If the day names in the parameter can be in any case, replace date.format(dtf).equals(day)
with date.format(dtf).equalsIgnoreCase(day)
in the code given above.
答案3
得分: 1
-
将您的 DaysOfWeek 输入字符串解析为
List<DayOfWeek>
对象。例如,首先调用.split("\\s*,\\s*")
来获取包含Mon
、Tue
等的字符串数组。 -
将字符串
"Mon"
转换为DayOfWeek.MONDAY
。如下所示。 -
创建一个循环以在开始和结束之间循环遍历每个日期:
for (LocalDate d = start; d.isBefore(end); d = d.plusDays(1))
。对于每个日期,获取它代表的星期几,并检查是否在您的列表中。
如何将 "Mon" 转换为 DOW.MONDAY?
您可以创建自己的哈希映射,将字符串映射到 DayOfWeek
值。或者,您可以依赖于 Java 的日期解析,但这有点复杂:
Locale where = Locale.forLanguageTag("en");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE", where);
TemporalAccessor accessor = formatter.parse("monday");
DayOfWeek dow = DayOfWeek.from(accessor);
请注意,您必须指定语言。毕竟,在全球范围内有很多种说法来表示“星期一”
英文:
-
Take your DaysOfWeek input string and parse it into a
List<DayOfWeek>
object. For example, by first calling.split("\\s*,\\s*")
to obtain a string array containingMon
,Tue
, etcetera. -
Turn the string
"Mon"
intoDayOfWeek.MONDAY
. See below. -
Create a for loop to loop through every date between start and end:
for (LocalDate d = start; d.isBefore(end); d = d.plusDays(1))
. Per date, fetch the day of week it represents, and check that it is in your list.
How do I turn "Mon" into DOW.MONDAY?
You can make your own hashmap that maps strings to DayOfWeek
values. Alternatively, you can rely on java's date parsing, but it's a little tricky:
Locale where = Locale.forLanguageTag("en");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE", where);
TemporalAccessor accessor = formatter.parse("monday");
DayOfWeek dow = DayOfWeek.from(accessor);
note how you'll have to specify language, of course. Across the planet there are quite a few ways to say 'monday', after all
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论