简体   繁体   English

Grails域类日期

[英]Grails Domain Classes Dates

For some reason, i would like to be able to add a reference date, like 0-0-0, which will represent 'no date'. 由于某种原因,我希望能够添加一个参考日期,例如0-0-0,它将表示“无日期”。 Any way of doing that ? 有什么办法吗? And will 0/0/0 work? 并且0/0/0可以工作吗?

If you want to represent "no date" that please do that by explicitly setting it to "no date" which in Groovy is simply done by assigning a null value to a date variable. 如果要表示“无日期”,请通过将其显式设置为“无日期”来实现,这在Groovy中只需为日期变量分配空值即可。

Date someDate = new Date()
print someDate //prints current date
someDate = null
print someDate //prints null - you can test for that

Does the date 0/0/0 represent a real point (or interval) in time? 日期0/0/0是否代表时间的真实点(或时间间隔)? In other words, if you had a desk Calendar that stretches infinitely far into the future and back into the past, could you find a page that corresponds to 0/0/0? 换句话说,如果您的桌面日历无限延伸到未来和过去,那么您是否可以找到对应于0/0/0的页面? If the answer is no, then this is not a valid Date value and therefore cannot be assigned to a Date variable. 如果答案为否,那么这不是有效的Date值,因此无法分配给Date变量。

On the other hand, if 0/0/0 represents a real point in time, eg the first day of the first year AD, then something like this might work 另一方面,如果0/0/0代表真实的时间点,例如,公元第一年的第一天,那么类似的事情可能会起作用

import static java.util.Calendar.*
def cal = Calendar.instance
cal[YEAR] = -1900
cal[MONTH] = 0
cal[DAY_OF_MONTH] = 1
cal.clearTime()

Date dayZero =  cal.time

However, my instinct is that this is a bad idea, and there's a better solution to the underlying problem that this weird Date usage. 但是,我的直觉是这是一个坏主意,并且有一个更好的解决方案来解决这种奇怪的Date使用问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM