gpt4 book ai didi

java - 如何确定一周中周五和周日之间特定时间的日期

转载 作者:行者123 更新时间:2023-12-01 18:13:42 24 4
gpt4 key购买 nike

我正在尝试使用 Java 检查当前日期和时间是否在周五 17:42 到周日 17:42 之间。

目前我正在使用非常非常糟糕的代码块来执行此操作。这是一个紧急的解决方案。现在我正在重构,但在 joda 等中找不到任何方法。

有什么想法吗?谢谢

private final Calendar currentDate = Calendar.getInstance();
private final int day = currentDate.get(Calendar.DAY_OF_WEEK);
private final int hour = currentDate.get(Calendar.HOUR_OF_DAY);
private final int minute = currentDate.get(Calendar.MINUTE);

if (day != 1 && day != 6 && day != 7) {
if (combined != 0) {
return badge == 1;
} else {
return badge == product;
}
} else {
if (day == 6 && hour > 16) {
if (hour == 17 && minute < 43) {
if (combined != 0) {
return badge == 1;
} else {
return badge == product;
}
} else {
return badge == 0;
}
} else if (day == 6 && hour < 17) {
if (combined != 0) {
return badge == 1;
} else {
return badge == product;
}
} else if (day == 1 && hour > 16) {
if (hour == 17 && minute < 43) {
return badge == 0;
} else {
if (combined != 0) {
return badge == 1;
} else {
return badge == product;
}
}
} else {
return badge == 0;
}
}

我在 @MadProgrammer 和 @Meno Hochschild 的帮助下使用了这样的解决方案

方法:

public static boolean isBetween(LocalDateTime check, LocalDateTime startTime, LocalDateTime endTime) {
return ((check.equals(startTime) || check.isAfter(startTime)) && (check.equals(endTime) || check.isBefore(endTime))); }

用法:

static LocalDateTime now = LocalDateTime.now();
static LocalDateTime friday = now.with(DayOfWeek.FRIDAY).toLocalDate().atTime(17, 41);
static LocalDateTime sunday = friday.plusDays(2).plusMinutes(1);

if (!isBetween(now, friday, sunday)) { ... }

再次感谢您的努力。

最佳答案

DateCalendar 具有可以对 Date/Calendar 的其他实例进行比较的方法>等于之前之后

但是,我鼓励使用 Java 8 的新 Time API

public static boolean isBetween(LocalDateTime check, LocalDateTime startTime, LocalDateTime endTime) {
return ((check.equals(startTime) || check.isAfter(startTime)) &&
(check.equals(endTime) || check.isBefore(endTime)));
}

如果提供的 LocalDateTime 位于指定范围内(含),则将返回 true

类似...

LocalDateTime start = LocalDateTime.now();
start = start.withDayOfMonth(26).withHour(17).withMinute(42).withSecond(0).withNano(0);
LocalDateTime end = start.plusDays(2);

LocalDateTime check = LocalDateTime.now();

System.out.println(check + " is within range = " + isBetween(check, start, end));
check = start;
System.out.println(check + " is within range = " + isBetween(check, start, end));
check = end;
System.out.println(check + " is within range = " + isBetween(check, start, end));
check = start.plusDays(1);
System.out.println(check + " is within range = " + isBetween(check, start, end));
check = end.plusMinutes(1);
System.out.println(check + " is within range = " + isBetween(check, start, end));

哪些输出

2015-06-25T18:31:32.969 is within range = false
2015-06-26T17:42 is within range = true
2015-06-28T17:42 is within range = true
2015-06-27T17:42 is within range = true
2015-06-28T17:43 is within range = false

Joda-Time 有一个 Interval 类,这使得它更加容易

Interval targetInterval = new Interval(targetStart, targetEnd);
System.out.println("Contains interval = " + interval.contains(targetInterval)

演示here

不同的方法...

所以我在回家的路上思考,假设你所拥有的只是你想要检查的日期/时间,你如何确定这一天是否在你的范围内

LocalDateTime now = LocalDateTime.now();
boolean isBetween = false;
switch (now.getDayOfWeek()) {
case FRIDAY:
case SATURDAY:
case SUNDAY:
LocalDateTime lastFriday = getLastFriday(now);
LocalDateTime nextSunday = getNextSunday(now);
isBetween = isBetween(now, lastFriday, nextSunday);
System.out.println(lastFriday + " - " + nextSunday + ": " + end);
break;
}

它的作用是检查dayOfWeek,看看它是否在所需的范围内,如果在,它会找到上一个星期五和下一个星期日 从指定日期开始并检查它是否在它们之间(请参阅前面的示例)

lastFridaynextSunday 只需从指定的日期/时间添加/减去day,直到达到所需的dayOfWeek code>,然后播种所需的时间限制

public static LocalDateTime getLastFriday(LocalDateTime anchor) {
LocalDateTime ldt = LocalDateTime.from(anchor);
return ldt.with(DayOfWeek.FRIDAY).withHour(17).withMinute(42).withSecond(0).withNano(0);
}

public static LocalDateTime getNextSunday(LocalDateTime anchor) {
LocalDateTime ldt = LocalDateTime.from(anchor);
return ldt.with(DayOfWeek.SUNDAY).withHour(17).withMinute(42).withSecond(0).withNano(0);
}

关于java - 如何确定一周中周五和周日之间特定时间的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31044827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com