grails - Groovy/Grails日期类-每月的某天

标签 grails groovy

目前,我正在使用以下代码来获取Groovy中的年,月,月,小时和分钟:

Date now = new Date()
Integer year = now.year + 1900
Integer month = now.month + 1
Integer day = now.getAt(Calendar.DAY_OF_MONTH) // inconsistent!
Integer hour = now.hours
Integer minute = now.minutes
// Code that uses year, month, day, hour and minute goes here

在这种情况下,在每月的某天使用getAt(Calendar.DAY_OF_MONTH)似乎有点不一致。有没有更短的方法来获取月份中的某天?

最佳答案

如果将以下内容添加到代码中,则应将一个月中的某天分配给Integer日:

Integer day = now.date

这是一个独立的示例:
def now = Date.parse("yyyy-MM-dd", "2009-09-15")
assert 15 == now.date

关于grails - Groovy/Grails日期类-每月的某天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1385680/

相关文章:

tomcat - 缺少 Grails spring security plugin 插件?

eclipse - Controller 中的 GGTS 重复方法名称/签名错误

jenkins - 从变量 Groovy 获取 YAML 值

java - 如何在Grails的脚手架 View 中显示ID列?

groovy - 有没有办法在 Groovy 中拥有可调用对象?

Groovy 'as' 关键字实现 2+ 接口(interface)

grails - Grails:解决 “Server failed to start for port 8080: Address already in use.”

java - 在Grails中导入/使用Java类

spring - 在没有 Controller 的情况下访问 GSP 中的 Spring bean

java - 如何在 groovy 中编写可写接口(interface)代码