有没有关于如何使用TimeCategory(或等效)java8 LocalDate和LocalDateTime的例子或参考?我找到的所有代码片段都引用了我试图避免的java.util.Date。Groovy的TimeCategory与LocalDate和LocalDateTime
回答
与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
}
一个简单的例子是:
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
}
是否有java8时间实体没有原生支持?您列出的示例虽然有效,但它是冗长的方式,并逐字杀死使用TimeCategory的这一点。 – Pierre
不,它没有任何。我同意这是冗长的。如果您正在寻找时间操作,那么LocalDateTime和LocalDate API已经有像'plusHours()'这样的方法。那是你正在寻找的东西吗? – dmahapatro
是的。 Java在我身上很强大:)并且完全知道所有本地日期和时间操作函数。然而,TimeCategory对我的目标受众(QA)提供了更好的吸引力。我想我会直接简单地java8时间 – Pierre
我也感到失望的是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')}"
- 1. Groovy的扩展TimeCategory类
- 2. 使用MOXy解组LocalDate/LocalDateTime
- 3. Joda时间从LocalDateTime字符串到LocalDate
- 4. 如何比较创建为JodaTime LocalDate和LocalDateTime的两个日期?
- 5. 比较LocalDate和Apache日期LocalDateTime的日期部分
- 6. 玩框架 - 与选项[LocalDate] \选项[LocalDateTime]使用范型
- 7. 在数据库中使用Java 8 LocalDate和LocalDateTime进行休眠
- 8. Groovy脚本沙箱:使用来自Java的Groovy TimeCategory语法作为字符串
- 9. 客户RestEasy的与LocalDateTime
- 10. 的Java 8 LocalDateTime和Hibernate 4
- 11. 在java的日期和LocalDateTime
- 12. 解析SQLDate,java.util.Date和LOCALDATE的
- 13. Spring Java JPD与Java 8 LocalDateTime
- 14. 创建从LocalDateTime新LocalDateTime和本地时间
- 15. LocalDateTime的JPA AttributeConverter
- 16. 与LOCALDATE构造约达说构造LOCALDATE的(INT,INT,INT)是不可见的
- 17. Groovy和Spring与Maven集成
- 18. Java从LocalDateTime栏减去LocalDateTime foo
- 19. LocalDate在LocalDate有私人访问
- 20. LocalDate和XMLGregorianCalendar之间的转换
- 21. 春天开机,杰克逊和LOCALDATE的
- 22. Week and WeekYear的LocalDate
- 23. 的Java格式LOCALDATE
- 24. TimeCategory使用变量增加分钟数
- 25. HSQLDB,LocalDateTime,JdbcTemplate
- 26. 毫秒到LocalDateTime
- 27. 使用LocalDateTime
- 28. 质疑LocalDateTime
- 29. LocalDateTime via Scannerinput
- 30. 对Java 8 LocalDateTime
看到我上面的评论。这完全是关于可读性和我的目标读者谁不是很流利的Java。 – Pierre
@Pierre您可以添加对LD和LDT的支持,而无需太多麻烦...查看编辑 –
完美!仍然不习惯groovy的动态特性,这是向外部甚至是最终类添加定制支持的一个很好的例子! – Pierre