Groovy java.time (LocalDate and LocalDateTime) compatible TimeCategory

| 1 min read

The following class upgrades the groovy wonderful TimeCategory to support the modern java8 java.time.LocalDate and java.time.LocalDateTime

class TimeCategory extends groovy.time.TimeCategory {
 public static LocalDate plus(final LocalDate date, final BaseDuration d) { return date.plusYears(d.years)
 .plusMonths(d.months)
 .plusDays(d.days);
 }

 public static LocalDate minus(final LocalDate date, final BaseDuration d) {
 return date.minusYears(d.years)
 .minusMonths(d.months)
 .minusDays(d.days);
 }

 public static LocalDateTime plus(final LocalDateTime dateTime, final BaseDuration d) {
 return dateTime.plusYears(d.years)
 .plusMonths(d.months)
 .plusDays(d.days)
 .plusHours(d.hours)
 .plusMinutes(d.minutes)
 .plusSeconds(d.seconds);
 }

 public static LocalDateTime minus(final LocalDateTime dateTime, final BaseDuration d) {
 return dateTime.plusYears(d.years)
 .minusMonths(d.months)
 .minusDays(d.days)
 .minusHours(d.hours)
 .minusMinutes(d.minutes)
 .minusSeconds(d.seconds);
 }
}