이미 질문을 받았을 것이라고 가정했지만 anything을 찾을 수 없습니다.ZonedDateTime이 "오늘"인지 확인하는 방법은 무엇입니까?
java.time
을 사용하면 주어진 ZonedDateTime
이 "오늘"인지 확인하는 가장 좋은 방법은 무엇입니까?
적어도 두 가지 가능한 해결책을 생각해 냈습니다. 이러한 접근법을 사용하는 허점이나 함정이 있는지 확실하지 않습니다.
/**
* @param zonedDateTime a zoned date time to compare with "now".
* @return true if zonedDateTime is "today".
* Where today is defined as year, month, and day of month being equal.
*/
public static boolean isZonedDateTimeToday1(ZonedDateTime zonedDateTime) {
ZonedDateTime now = ZonedDateTime.now();
return now.getYear() == zonedDateTime.getYear()
&& now.getMonth() == zonedDateTime.getMonth()
&& now.getDayOfMonth() == zonedDateTime.getDayOfMonth();
}
/**
* @param zonedDateTime a zoned date time to compare with "now".
* @return true if zonedDateTime is "today".
* Where today is defined as atStartOfDay() being equal.
*/
public static boolean isZoneDateTimeToday2(ZonedDateTime zonedDateTime) {
ZonedDateTime now = ZonedDateTime.now();
LocalDateTime atStartOfToday = now.toLocalDate().atStartOfDay();
LocalDateTime atStartOfDay = zonedDateTime.toLocalDate().atStartOfDay();
return atStartOfDay == atStartOfToday;
}
값의 시간대가 "오늘"이거나 JVM의 기본 시간대 인 값을 묻는 중입니까? – Andreas
ZonedDateTime now = ZonedDateTime.now()라고 말하면 안됩니다. 그것은 항상 현재 날짜입니다. 항상 오늘을 의미합니다. – Sedrick
먼저 "오늘"이 의미하는 바를 절대적으로 분명하게 말하십시오! – slim