Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy's TimeCategory with LocalDate and LocalDateTime

Tags:

java-8

groovy

Are there examples or references on how to use TimeCategory (or equivalent) with java8 LocalDate and LocalDateTime? All of the code snippets I found refer to java.util.Date which I am trying to avoid.

like image 635
Pierre Avatar asked Mar 29 '16 20:03

Pierre


3 Answers

With Java 8 LocalDate manipulation being as simple as:

LocalDate.now().plusDays(2)

I'm not sure what TimeCategory would gain you?


You can hack it in to the metaClass of LocalDate and LocalDatTime quite simply:

import groovy.time.*
import java.time.*

LocalDate.metaClass {
    plus { Duration d ->
        delegate.plusYears(d.years)
                .plusMonths(d.months)
                .plusDays(d.days)
    }
}

LocalDateTime.metaClass {
    plus { Duration d ->
        delegate.plusYears(d.years)
                .plusMonths(d.months)
                .plusDays(d.days)
                .plusHours(d.hours)
                .plusMinutes(d.minutes)
                .plusSeconds(d.seconds)
    }
}

use(TimeCategory) {
    LocalDateTime.now() + 4.days
}
like image 123
tim_yates Avatar answered Oct 13 '22 13:10

tim_yates


A trivial example would be:

import groovy.time.TimeCategory
import java.time.LocalDate
import java.time.LocalDateTime

use( TimeCategory ) {
    println Date.parse('yyyy-MM-dd', LocalDate.now().toString()) + 4.hours
    println Date.parse("yyyy-MM-dd'T'hh:mm:ss", LocalDateTime.now().toString()) - 4.hours
}
like image 28
dmahapatro Avatar answered Oct 13 '22 14:10

dmahapatro


I too was disappointed that TimeCategory declares it's own duration and isn't Java 8 friendly. This prompted me to write a bit of my own code which I thought would be relevant to this question. It's focused around ZonedDateTime as opposed to LocalDateTime because I was interested in TimeZone logic where I'm using it. It's not nearly as complete as the groovy.time.TimeCategory class, and has just the handful of operations I was interested in, so feel free to add to it.

Without further ado:

import java.time.Duration
import java.time.ZoneOffset
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit

class TimeCategory {
    static getDays(Integer self) { Duration.ofDays self }
    static getHours(Integer self) { Duration.ofHours self }
    static getMillis(Integer self) { Duration.ofMillis self }
    static getMinutes(Integer self) { Duration.ofMinutes self }
    static getNanos(Integer self) { Duration.ofNanos self }
    static getSeconds(Integer self) { Duration.ofSeconds self }
    static getWeeks(Integer self) { Duration.ofDays self * 7 }
    static getDay(Integer self) { Duration.ofDays self }
    static getHour(Integer self) { Duration.ofHours self }
    static getMilli(Integer self) { Duration.ofMillis self }
    static getMinute(Integer self) { Duration.ofMinutes self }
    static getNano(Integer self) { Duration.ofNanos self }
    static getSecond(Integer self) { Duration.ofSeconds self }
    static getWeek(Integer self) { Duration.ofDays self * 7 }
    static ZonedDateTime getAgo(Duration self) { ZonedDateTime.now() - self }
    static ZonedDateTime getUtc(ZonedDateTime self) { self.withZoneSameInstant(ZoneOffset.UTC) }
    static ZonedDateTime getLocal(ZonedDateTime self) { self.withZoneSameInstant(ZoneOffset.systemDefault()) }
    static ZonedDateTime getNow() { ZonedDateTime.now() }
    static Duration minus(ZonedDateTime self, ZonedDateTime other) { Duration.between(self, other) }
    static BigInteger getTotalNanos(Duration self) { self.seconds.toBigInteger() * 10 ** 9 + self.nano }
    static String toString(ZonedDateTime self, String pattern) { self.format(DateTimeFormatter.ofPattern(pattern)) }
    static Duration mod(Duration self, Duration other) {
        def (long seconds, int nanos) = (self.totalNanos % other.totalNanos).divideAndRemainder(10g.pow(9))
        Duration.ofSeconds(seconds) + nanos.nanos
    }

    static load() {
        Integer.mixin(TimeCategory)
        ZonedDateTime.mixin(TimeCategory)
        Duration.mixin(TimeCategory)
    }
}

Example usage:

// I prefer putting this in a start-up location and pollute the namespace rather than use 
// `use() {...}`
TimeCategory.load()

// Construct readable java 8 durations
Duration d = 1.day + 2.hours + 3.minutes - 4.seconds

// Easily construct dates relative to now
ZonedDateTime yesterday = 1.day.ago

// Of course, you can still call "unsugared" functions like truncatedTo for rounding down
ZonedDateTime today = TimeCategory.now.truncatedTo(ChronoUnit.DAYS)

// Converting between local and utc doesn't change the underlying instant
assert 0.seconds == yesterday.utc - yesterday.local

// Modulo arithmetic.  Cool!
assert 1.minute % 17.seconds == 9.seconds

// Slightly cleaner date formatting
println "Today in strange format ${today.toString('dd/yy/MM')}"
like image 33
Marty Neal Avatar answered Oct 13 '22 13:10

Marty Neal