2016-03-29 70 views

回答

3

與Java 8操縱LOCALDATE是簡單的:

LocalDate.now().plusDays(2) 

我不知道什麼TimeCategory將獲得嗎?


您可以到和LOCALDATE相當LocalDatTime簡單元類本事:

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 
} 
+0

看到我上面的評論。這完全是關於可讀性和我的目標讀者誰不是很流利的Java。 – Pierre

+1

@Pierre您可以添加對LD和LDT的支持,而無需太多麻煩...查看編輯 –

+0

完美!仍然不習慣groovy的動態特性,這是向外部甚至是最終類添加定製支持的一個很好的例子! – Pierre

2

一個簡單的例子是:

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 
} 
+0

是否有java8時間實體沒有原生支持?您列出的示例雖然有效,但它是冗長的方式,並逐字殺死使用TimeCategory的這一點。 – Pierre

+1

不,它沒有任何。我同意這是冗長的。如果您正在尋找時間操作,那麼LocalDateTime和LocalDate API已經有像'plusHours()'這樣的方法。那是你正在尋找的東西嗎? – dmahapatro

+0

是的。 Java在我身上很強大:)並且完全知道所有本地日期和時間操作函數。然而,TimeCategory對我的目標受衆(QA)提供了更好的吸引力。我想我會直接簡單地java8時間 – Pierre

2

我也感到失望的是TimeCategory宣佈它自己的時間,是不是Java 8友好。這促使我寫了一些我自己的代碼,我認爲這與這個問題有關。它的重點在於ZonedDateTime而不是LocalDateTime,因爲我對TimeZone邏輯感興趣,我正在使用它。它並不像groovy.time.TimeCategory這樣完整,只有少數我感興趣的操作,所以隨時添加它。

事不宜遲:

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) 
    } 
} 

用法示例:

// 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')}" 
+0

謝謝!絕對有用的代碼有。 – Pierre

+0

你會如何修改這個以支持.from.now,就像默認的TimeCategory一樣? – Ali