[英]How to display all Mondays between two dates?
Im trying display all Mondays between two dates but I have no idea how to do it.我尝试在两个日期之间显示所有星期一,但我不知道该怎么做。 I want ask user to input two dates using Scanner, to output all Mondays.
我想要求用户在所有星期一使用扫描仪输入两个日期到 output。
String s = "2020-07-20";
String e = "2020-09-20";
LocalDate start = LocalDate.parse(s);
LocalDate end = LocalDate.parse(e);
List<LocalDate> totalDates = new ArrayList<>();
while (!start.isAfter(end)) {
totalDates.add(start);
start = start.plusDays(1);
}
To get the the first Monday, use:要获得第一个星期一,请使用:
LocalDate monday = start.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));
Now you have to deal with an edge case: What if there is no Monday between start
and end
?现在您必须处理一个极端情况:如果
start
和end
之间没有星期一怎么办? That would mean that the Monday computed here is after end
:这意味着这里计算的星期一在
end
之后:
if (monday.isAfter(end)) {
totalDates = List.of();
}
After that, you can get a sequence of Mondays with the convenient datesUntil
method:之后,您可以使用方便的
datesUntil
方法获得一系列星期一:
totalDates = monday.datesUntil(end, Period.ofWeeks(1)).collect(Collectors.toList());
Note that datesUntil
does not include the end date.请注意
datesUntil
不包括结束日期。 If you need the end date included, pass in end.plusDays(1)
.如果您需要包含结束日期,请传入
end.plusDays(1)
。
You can use .getDayOfWeek()
to get DayOfWeek
and compare with DayOfWeek.MONDAY
您可以使用
.getDayOfWeek()
获取DayOfWeek
并与DayOfWeek.MONDAY
进行比较
while (!start.isAfter(end)) {
if(start.getDayOfWeek().equals(DayOfWeek.MONDAY)) {
totalDates.add(start);
}
start = start.plusDays(1);
}
And optimization is use start.plusWeeks(1)
instead of start.plusDays(1)
and you need to get the next monday before.并且优化是使用
start.plusWeeks(1)
而不是start.plusDays(1)
并且您需要在下一个星期一之前获得。
start = start.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));
while (!start.isAfter(end)) {
if(start.getDayOfWeek().equals(DayOfWeek.MONDAY)) {
totalDates.add(start);
}
start = start.plusWeeks(1);
}
You need to use getDayOfWeek()
, here is the solution:您需要使用
getDayOfWeek()
,这是解决方案:
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
public class AllMondays {
public static void main(String[] args) {
String s = "2020-07-20";
String e = "2020-09-20";
LocalDate start = LocalDate.parse(s);
LocalDate end = LocalDate.parse(e);
List<LocalDate> totalDates = new ArrayList<>();
LocalDate nextMonday = start;
int daysToAdvance = 1;
while (!nextMonday.isAfter(end)) {
if (nextMonday.getDayOfWeek() == DayOfWeek.MONDAY) {
daysToAdvance = 7;
totalDates.add(nextMonday);
}
nextMonday = nextMonday.plusDays(daysToAdvance);
}
System.out.println(totalDates);
}
}
Here "find_day" is the day you want to find in date range这里的“find_day”是您要在日期范围内查找的日期
SimpleDateFormat format1 = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
Date sub_start_date = format1.parse("01-06-2021");
Date enddate = format1.parse("30-06-2021");
Calendar min_date_c = Calendar.getInstance();
min_date_c.setTime(sub_start_date);
Calendar max_date_c = Calendar.getInstance();
max_date_c.setTime(enddate);
for (Calendar loopdate = min_date_c; min_date_c.before(max_date_c); min_date_c.add(Calendar.DATE, 1), loopdate = min_date_c) {
int dayOfWeek = loopdate.get(Calendar.DAY_OF_WEEK);
// if (dayOfWeek == Calendar.MONDAY || dayOfWeek == Calendar.SATURDAY) {
if (dayOfWeek == find_day) {
Calendar[] disabledDays = new Calendar[1];
disabledDays[0] = loopdate;
Date newdate = loopdate.getTime();
String strDate = format1.format(newdate);
Log.e("DATES>>> ", "" + strDate);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.