forked from plusone/plusone-commons
feat: DateTimeTools 新增 isFuture 和 isPast。
This commit is contained in:
parent
29519f3489
commit
a9c8fec81a
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023-2024 the original author or authors.
|
* Copyright 2023-2025 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -722,6 +722,78 @@ public class DateTimeTools {
|
|||||||
// #endregion - start & end
|
// #endregion - start & end
|
||||||
// ================================
|
// ================================
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #region - isFuture
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
public static boolean isFuture(Date date) {
|
||||||
|
return date.after(new Date());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isFuture(Calendar date) {
|
||||||
|
return date.after(Calendar.getInstance());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isFuture(Instant instant) {
|
||||||
|
return instant.isAfter(Instant.now());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isFuture(long timeMillis) {
|
||||||
|
return timeMillis > System.currentTimeMillis();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isFuture(LocalDate date) {
|
||||||
|
return date.isAfter(LocalDate.now());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isFuture(LocalDateTime dateTime) {
|
||||||
|
return dateTime.isAfter(LocalDateTime.now());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isFuture(ZonedDateTime dateTime) {
|
||||||
|
return dateTime.isAfter(ZonedDateTime.now());
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #endregion - isFuture
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #region - isPast
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
public static boolean isPast(Date date) {
|
||||||
|
return date.before(new Date());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isPast(Calendar date) {
|
||||||
|
return date.before(Calendar.getInstance());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isPast(Instant instant) {
|
||||||
|
return instant.isBefore(Instant.now());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isPast(long timeMillis) {
|
||||||
|
return timeMillis < System.currentTimeMillis();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isPast(LocalDate date) {
|
||||||
|
return date.isBefore(LocalDate.now());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isPast(LocalDateTime dateTime) {
|
||||||
|
return dateTime.isBefore(LocalDateTime.now());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isPast(ZonedDateTime dateTime) {
|
||||||
|
return dateTime.isBefore(ZonedDateTime.now());
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #endregion - isPast
|
||||||
|
// ================================
|
||||||
|
|
||||||
// ================================
|
// ================================
|
||||||
// #region - others
|
// #region - others
|
||||||
// ================================
|
// ================================
|
||||||
|
@ -435,32 +435,117 @@ class DateTimeToolsTests {
|
|||||||
// ================================
|
// ================================
|
||||||
|
|
||||||
// ================================
|
// ================================
|
||||||
// #region - others
|
// #region - start & end
|
||||||
// ================================
|
// ================================
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void startDateOfYear() {
|
void testStartAndEndDateOfYear() {
|
||||||
assertEquals(LocalDate.of(2008, 1, 1), DateTimeTools.startDateOfYear(2008));
|
assertEquals(LocalDate.of(2008, 1, 1), DateTimeTools.startDateOfYear(2008));
|
||||||
assertEquals(LocalDate.of(2008, 12, 31), DateTimeTools.endDateOfYear(2008));
|
assertEquals(LocalDate.of(2008, 12, 31), DateTimeTools.endDateOfYear(2008));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testStartOfNextDate() {
|
||||||
assertEquals(LocalDateTime.of(2024, 12, 30, 0, 0, 0),
|
assertEquals(LocalDateTime.of(2024, 12, 30, 0, 0, 0),
|
||||||
DateTimeTools.startOfNextDate(LOCAL_DATE));
|
DateTimeTools.startOfNextDate(LOCAL_DATE));
|
||||||
assertEquals(LocalDateTime.of(2024, 12, 30, 0, 0, 0).atZone(SYS_ZONE_ID),
|
assertEquals(LocalDateTime.of(2024, 12, 30, 0, 0, 0).atZone(SYS_ZONE_ID),
|
||||||
DateTimeTools.startOfNextDate(LOCAL_DATE, SYS_ZONE_ID));
|
DateTimeTools.startOfNextDate(LOCAL_DATE, SYS_ZONE_ID));
|
||||||
assertEquals(LocalDateTime.of(2024, 12, 30, 0, 0, 0).atZone(ZONE_ID),
|
assertEquals(LocalDateTime.of(2024, 12, 30, 0, 0, 0).atZone(ZONE_ID),
|
||||||
DateTimeTools.startOfNextDate(LOCAL_DATE, ZONE_ID));
|
DateTimeTools.startOfNextDate(LOCAL_DATE, ZONE_ID));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #endregion - start & end
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #region - isFuture & isPast
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void test_isFuture_And_isPast_WhenFuture() {
|
||||||
|
Date date = new Date(Instant.now().plusSeconds(10).toEpochMilli());
|
||||||
|
assertTrue(DateTimeTools.isFuture(date));
|
||||||
|
assertFalse(DateTimeTools.isPast(date));
|
||||||
|
|
||||||
|
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("+01:00"));
|
||||||
|
calendar.add(Calendar.SECOND, 10);
|
||||||
|
assertTrue(DateTimeTools.isFuture(calendar));
|
||||||
|
assertFalse(DateTimeTools.isPast(calendar));
|
||||||
|
|
||||||
|
Instant instant = Instant.now().plusSeconds(10);
|
||||||
|
assertTrue(DateTimeTools.isFuture(instant));
|
||||||
|
assertFalse(DateTimeTools.isPast(instant));
|
||||||
|
|
||||||
|
long timeMillis = Instant.now().plusSeconds(10).toEpochMilli();
|
||||||
|
assertTrue(DateTimeTools.isFuture(timeMillis));
|
||||||
|
assertFalse(DateTimeTools.isPast(timeMillis));
|
||||||
|
|
||||||
|
LocalDate localDate = LocalDate.now().plusDays(1);
|
||||||
|
assertTrue(DateTimeTools.isFuture(localDate));
|
||||||
|
assertFalse(DateTimeTools.isPast(localDate));
|
||||||
|
|
||||||
|
LocalDateTime localDateTime = LocalDateTime.now().plusSeconds(10);
|
||||||
|
assertTrue(DateTimeTools.isFuture(localDateTime));
|
||||||
|
assertFalse(DateTimeTools.isPast(localDateTime));
|
||||||
|
|
||||||
|
ZonedDateTime zonedDateTime = Instant.now().plusSeconds(10).atZone(ZoneId.of("+01:00"));
|
||||||
|
assertTrue(DateTimeTools.isFuture(zonedDateTime));
|
||||||
|
assertFalse(DateTimeTools.isPast(zonedDateTime));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void test_isFuture_And_isPast_WhenPast() {
|
||||||
|
Date date = new Date(Instant.now().minusSeconds(10).toEpochMilli());
|
||||||
|
assertFalse(DateTimeTools.isFuture(date));
|
||||||
|
assertTrue(DateTimeTools.isPast(date));
|
||||||
|
|
||||||
|
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("+01:00"));
|
||||||
|
calendar.add(Calendar.SECOND, -10);
|
||||||
|
assertFalse(DateTimeTools.isFuture(calendar));
|
||||||
|
assertTrue(DateTimeTools.isPast(calendar));
|
||||||
|
|
||||||
|
Instant instant = Instant.now().minusSeconds(10);
|
||||||
|
assertFalse(DateTimeTools.isFuture(instant));
|
||||||
|
assertTrue(DateTimeTools.isPast(instant));
|
||||||
|
|
||||||
|
long timeMillis = Instant.now().minusSeconds(10).toEpochMilli();
|
||||||
|
assertFalse(DateTimeTools.isFuture(timeMillis));
|
||||||
|
assertTrue(DateTimeTools.isPast(timeMillis));
|
||||||
|
|
||||||
|
LocalDate localDate = LocalDate.now().minusDays(1);
|
||||||
|
assertFalse(DateTimeTools.isFuture(localDate));
|
||||||
|
assertTrue(DateTimeTools.isPast(localDate));
|
||||||
|
|
||||||
|
LocalDateTime localDateTime = LocalDateTime.now().minusSeconds(10);
|
||||||
|
assertFalse(DateTimeTools.isFuture(localDateTime));
|
||||||
|
assertTrue(DateTimeTools.isPast(localDateTime));
|
||||||
|
|
||||||
|
ZonedDateTime zonedDateTime = Instant.now().minusSeconds(10).atZone(ZoneId.of("+01:00"));
|
||||||
|
assertFalse(DateTimeTools.isFuture(zonedDateTime));
|
||||||
|
assertTrue(DateTimeTools.isPast(zonedDateTime));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #endregion - isFuture & isPast
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #region - others
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void startDateTimeRange() {
|
||||||
Range<LocalDateTime> localDateTimeRange = DateTimeTools.toDateTimeRange(LOCAL_DATE);
|
Range<LocalDateTime> localDateTimeRange = DateTimeTools.toDateTimeRange(LOCAL_DATE);
|
||||||
assertEquals(LOCAL_DATE.atStartOfDay(), localDateTimeRange.lowerEndpoint());
|
assertEquals(LOCAL_DATE.atStartOfDay(), localDateTimeRange.lowerEndpoint());
|
||||||
|
assertEquals(LocalDate.of(2024, 12, 30).atStartOfDay(), localDateTimeRange.upperEndpoint());
|
||||||
assertTrue(localDateTimeRange.contains(LOCAL_DATE.atStartOfDay()));
|
assertTrue(localDateTimeRange.contains(LOCAL_DATE.atStartOfDay()));
|
||||||
assertEquals(LocalDate.of(2024, 12, 30).atStartOfDay(), localDateTimeRange.upperEndpoint());
|
|
||||||
assertEquals(LocalDate.of(2024, 12, 30).atStartOfDay(), localDateTimeRange.upperEndpoint());
|
|
||||||
assertFalse(localDateTimeRange.contains(LocalDate.of(2024, 12, 30).atStartOfDay()));
|
assertFalse(localDateTimeRange.contains(LocalDate.of(2024, 12, 30).atStartOfDay()));
|
||||||
|
|
||||||
Range<ZonedDateTime> zonedDateTimeRange = DateTimeTools.toDateTimeRange(LOCAL_DATE, SYS_ZONE_ID);
|
Range<ZonedDateTime> zonedDateTimeRange = DateTimeTools.toDateTimeRange(LOCAL_DATE, SYS_ZONE_ID);
|
||||||
assertEquals(LOCAL_DATE.atStartOfDay().atZone(SYS_ZONE_ID), zonedDateTimeRange.lowerEndpoint());
|
assertEquals(LOCAL_DATE.atStartOfDay().atZone(SYS_ZONE_ID), zonedDateTimeRange.lowerEndpoint());
|
||||||
|
assertEquals(LocalDate.of(2024, 12, 30).atStartOfDay().atZone(SYS_ZONE_ID), zonedDateTimeRange.upperEndpoint());
|
||||||
assertTrue(zonedDateTimeRange.contains(LOCAL_DATE.atStartOfDay().atZone(SYS_ZONE_ID)));
|
assertTrue(zonedDateTimeRange.contains(LOCAL_DATE.atStartOfDay().atZone(SYS_ZONE_ID)));
|
||||||
assertEquals(ZonedDateTime.of(LocalDate.of(2024, 12, 30).atStartOfDay(), SYS_ZONE_ID), zonedDateTimeRange.upperEndpoint());
|
|
||||||
assertEquals(ZonedDateTime.of(LocalDate.of(2024, 12, 30).atStartOfDay(), SYS_ZONE_ID), zonedDateTimeRange.upperEndpoint());
|
|
||||||
assertFalse(zonedDateTimeRange.contains(LocalDate.of(2024, 12, 30).atStartOfDay().atZone(SYS_ZONE_ID)));
|
assertFalse(zonedDateTimeRange.contains(LocalDate.of(2024, 12, 30).atStartOfDay().atZone(SYS_ZONE_ID)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -480,6 +565,10 @@ class DateTimeToolsTests {
|
|||||||
assertThrows(DateTimeException.class, () -> DateTimeTools.toYearString(Year.MIN_VALUE - 1));
|
assertThrows(DateTimeException.class, () -> DateTimeTools.toYearString(Year.MIN_VALUE - 1));
|
||||||
assertThrows(DateTimeException.class, () -> DateTimeTools.toYearString(Year.MAX_VALUE + 1));
|
assertThrows(DateTimeException.class, () -> DateTimeTools.toYearString(Year.MAX_VALUE + 1));
|
||||||
|
|
||||||
|
assertEquals("2024", DateTimeTools.toYearString(Year.of(2024)));
|
||||||
|
assertEquals("999999999", DateTimeTools.toYearString(Year.of(Year.MAX_VALUE)));
|
||||||
|
assertEquals("-999999999", DateTimeTools.toYearString(Year.of(Year.MIN_VALUE)));
|
||||||
|
|
||||||
assertEquals("01", DateTimeTools.toMonthStringMM(1));
|
assertEquals("01", DateTimeTools.toMonthStringMM(1));
|
||||||
assertEquals("02", DateTimeTools.toMonthStringMM(2));
|
assertEquals("02", DateTimeTools.toMonthStringMM(2));
|
||||||
assertEquals("3", DateTimeTools.toMonthStringM(3));
|
assertEquals("3", DateTimeTools.toMonthStringM(3));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user