试图从月历中跳过周末
Calendar cal = Calendar.getInstance();
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
for (int i = 0; i < dayOfMonth; i++) {
id("prevDayTrackButton").click();
Thread.sleep(1000);
}
答案 0 :(得分:2)
int dayOfWeek = cal.get (Calendar.DAY_OF_WEEK);
boolean isWeekday = ((dayOfWeek >= Calendar.MONDAY) && (dayOfWeek <= Calendar.FRIDAY));
如果星期几是isWeekday
或false
,则 Saturday
将是Sunday
。
答案 1 :(得分:0)
Calendar cal = Calendar.getInstance();
boolean isWeekend;
if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY ||
cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
isWeekend = true;
}
答案 2 :(得分:0)
LocalDate today = LocalDate.now(ZoneId.of("Pacific/Wallis"));
for (LocalDate d = today.withDayOfMonth(1); d.isBefore(today); d = d.plusDays(1)) {
DayOfWeek dow = d.getDayOfWeek();
if (! dow.equals(DayOfWeek.SATURDAY) && ! dow.equals(DayOfWeek.SUNDAY)) {
id("prevDayTrackButton").click();
TimeUnit.SECONDS.sleep(1);
}
}
如果您喜欢使用流操作:
today.withDayOfMonth(1)
.datesUntil(today)
.filter(d -> ! d.getDayOfWeek().equals(DayOfWeek.SATURDAY)
&& ! d.getDayOfWeek().equals(DayOfWeek.SUNDAY))
.forEach(d -> {
id("prevDayTrackButton").click();
TimeUnit.SECONDS.sleep(1);
});
在这种情况下,我认为它不会给我们买任何东西。
您使用的Calendar
类早已过时且设计不良。相反,我使用的是现代Java日期和时间API java.time。
链接: Oracle tutorial: Date Time解释了如何使用java.time。
答案 3 :(得分:0)
LocalDate myDate = LocalDate.of(2019, 4, 13);//LocalDate.parse("2019-04-13");
DayOfWeek dayOfWeek = myDate.getDayOfWeek();
int dayOfWeekIntValue = dayOfWeek.getValue();
System.out.println(dayOfWeekIntValue);
String dayOfWeekName = dayOfWeek.name();
System.out.println(dayOfWeekName);
boolean isWeekday = (dayOfWeekIntValue >= 1 && dayOfWeekIntValue <= 5);//Day is between MONDAY and FRIDAY, return true
System.out.println(isWeekday);
使用Date
代替旧的LocalDate
类。