From bf120b44ab1fba03c2dbb8bb43305042ddbce22f Mon Sep 17 00:00:00 2001 From: Swetank Mohanty Date: Mon, 17 Jun 2024 23:24:13 +0530 Subject: [PATCH] Added new utility methods for DateUtils --- .../primekit/essentials/common/YearWeek.java | 32 ++++++ .../essentials/common/util/DateUtils.java | 97 +++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 primekit-essentials/src/main/java/com/shortthirdman/primekit/essentials/common/YearWeek.java diff --git a/primekit-essentials/src/main/java/com/shortthirdman/primekit/essentials/common/YearWeek.java b/primekit-essentials/src/main/java/com/shortthirdman/primekit/essentials/common/YearWeek.java new file mode 100644 index 0000000..ab26b18 --- /dev/null +++ b/primekit-essentials/src/main/java/com/shortthirdman/primekit/essentials/common/YearWeek.java @@ -0,0 +1,32 @@ +package com.shortthirdman.primekit.essentials.common; + +import java.text.MessageFormat; +import java.time.DateTimeException; +import java.time.LocalDate; +import java.time.chrono.Chronology; +import java.time.chrono.IsoChronology; +import java.time.temporal.ChronoField; +import java.time.temporal.TemporalAccessor; +import java.util.Objects; + + +public record YearWeek(int year, int week) { + + public static YearWeek from(TemporalAccessor temporal) { + Objects.requireNonNull(temporal, "temporal"); + try { + if (!IsoChronology.INSTANCE.equals(Chronology.from(temporal))) { + temporal = LocalDate.from(temporal); + } + return new YearWeek(temporal.get(ChronoField.YEAR), temporal.get(ChronoField.ALIGNED_WEEK_OF_YEAR)); + } catch (DateTimeException ex) { + String mf = MessageFormat.format("Unable to obtain YearWeek from TemporalAccessor: {0} of type {1}", temporal, temporal.getClass().getName()); + throw new DateTimeException(mf, ex); + } + } + + @Override + public String toString() { + return MessageFormat.format("{0}-{1}", year, week); + } +} diff --git a/primekit-essentials/src/main/java/com/shortthirdman/primekit/essentials/common/util/DateUtils.java b/primekit-essentials/src/main/java/com/shortthirdman/primekit/essentials/common/util/DateUtils.java index b66f207..6a9128c 100644 --- a/primekit-essentials/src/main/java/com/shortthirdman/primekit/essentials/common/util/DateUtils.java +++ b/primekit-essentials/src/main/java/com/shortthirdman/primekit/essentials/common/util/DateUtils.java @@ -1,5 +1,6 @@ package com.shortthirdman.primekit.essentials.common.util; +import com.shortthirdman.primekit.essentials.common.YearWeek; import org.apache.commons.lang3.StringUtils; import java.sql.Timestamp; @@ -445,4 +446,100 @@ public static Integer getQuarterNumber(LocalDate date) { return date.get(IsoFields.QUARTER_OF_YEAR); } + + /** + * Split date-time range into equal intervals + * @param start the start date + * @param end the end date + * @param n the intervals + * @return list of date-time + */ + public static List splitDateTimeRange(LocalDateTime start, LocalDateTime end, int n) { + if (start == null || end == null) { + throw new IllegalArgumentException("Start date or end date can not be null"); + } + + Duration range = Duration.between(start, end); + Duration interval = range.dividedBy(n - 1); + List listOfDates = new ArrayList<>(); + LocalDateTime timeline = start; + for (int i = 0; i < n - 1; i++) { + listOfDates.add(timeline); + timeline = timeline.plus(interval); + } + listOfDates.add(end); + return listOfDates; + } + + /** + * Split date-time range into days + * @param start the start date + * @param end the end date + * @return list of date + */ + public static List splitDateRangeIntoDays(LocalDate start, LocalDate end) { + if (start == null || end == null) { + throw new IllegalArgumentException("Start date or end date can not be null"); + } + + long numOfDaysBetween = ChronoUnit.DAYS.between(start, end); + return IntStream.iterate(0, i -> i + 1) + .limit(numOfDaysBetween) + .mapToObj(start::plusDays) + .collect(Collectors.toList()); + } + + /** + * Split date-time range into months + * @param start the start date + * @param end the end date + * @return list of year-month + */ + public static List splitDateRangeIntoMonths(LocalDate start, LocalDate end) { + if (start == null || end == null) { + throw new IllegalArgumentException("Start date or end date can not be null"); + } + + long numOfDaysBetween = ChronoUnit.MONTHS.between(start, end); + return IntStream.iterate(0, i -> i + 1) + .limit(numOfDaysBetween) + .mapToObj(i -> YearMonth.from(start.plusMonths(i))) + .collect(Collectors.toList()); + } + + /** + * Split date-time range into years + * @param start the start date + * @param end the end date + * @return the list of years + */ + public static List splitDateRangeIntoYears(LocalDate start, LocalDate end) { + if (start == null || end == null) { + throw new IllegalArgumentException("Start date or end date can not be null"); + } + + long numOfDaysBetween = ChronoUnit.YEARS.between(start, end); + return IntStream.iterate(0, i -> i + 1) + .limit(numOfDaysBetween) + .mapToObj(i -> Year.from(start.plusYears(i))) + .collect(Collectors.toList()); + } + + /** + * Split date-time range into weeks + * @param start the start date + * @param end the end date + * @return the list of year-week + */ + public static List splitDateRangeIntoWeeks(LocalDate start, LocalDate end) { + if (start == null || end == null) { + throw new IllegalArgumentException("Start date or end date can not be null"); + } + + long numOfDaysBetween = ChronoUnit.WEEKS.between(start, end); + return IntStream.iterate(0, i -> i + 1) + .limit(numOfDaysBetween) + .mapToObj(i -> YearWeek.from(start.plusWeeks(i))) + .collect(Collectors.toList()); + } }