开发者

Grails Domain Classes Dates

开发者 https://www.devze.com 2023-03-06 16:47 出处:网络
For some reason, i would like to be able to add a refer开发者_JAVA技巧ence date, like 0-0-0, which will represent \'no date\'. Any way of doing that ? And will 0/0/0 work?If you want to represent \"no

For some reason, i would like to be able to add a refer开发者_JAVA技巧ence date, like 0-0-0, which will represent 'no date'. Any way of doing that ? And will 0/0/0 work?


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.

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? 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? If the answer is no, then this is not a valid Date value and therefore cannot be assigned to a Date variable.

On the other hand, if 0/0/0 represents a real point in time, e.g. the first day of the first year AD, then something like this might work

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消