Skip to content

Commit

Permalink
Added new utility methods for DateUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
shortthirdman committed Jun 17, 2024
1 parent ad99231 commit bf120b4
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<LocalDateTime> 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<LocalDateTime> 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<LocalDate> 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<YearMonth> 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<Year> 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<YearWeek> 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());
}
}

0 comments on commit bf120b4

Please sign in to comment.