Currently I'm using the following code to get year, month, day of month, hour and minute in 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
Using getAt(Calendar.DAY_OF_MONTH)
for the day of the month seems a bit inconsistent in this context. Is there any shorter way to obtain the day of the month?
If you add the following to your code it should assign the day of the month to the day Integer:
Integer day = now.date
Here's a stand-alone example:
def now = Date.parse("yyyy-MM-dd", "2009-09-15")
assert 15 == now.date
You can get just the day of the month from a Date
in Groovy like this:
Date date = new Date()
int dayOfMonth = date[Calendar.DAY_OF_MONTH]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With