Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy get today's date at minute 00:00:00

Tags:

date

groovy

I have this problem. I need to do the following:

  • get todays date
  • make a new date which will be today's date at 00:00:00
  • make another date which will be today's date at 23:59:59

For example. Today Date is 12-January-2012 19:00

How can i make a new date, which will be 12-January-2012 00:00 (the start of the current day)

It may seems easy, but i couldnt find any groovyway to get it, any help would be apreciated.

like image 817
recoInrelax Avatar asked Jan 12 '12 19:01

recoInrelax


1 Answers

To get the date at midnight use Date.clearTime (docs):

dateAtMidnight = new Date()
dateAtMidnight.clearTime()

(Javadocs are for Groovy JDK < 2.0, clearTime() is declared void in Groovy JDK 2.0, preventing d = new Date().clearTime(). Comments indicate the original functionality may be restored, yay!)

For the comparison, instead of using <= 23:59:59, use < (the next day):

(aDate >= dateAtMidnight) && (aDate < (dateAtMidnight + 1))
like image 145
Dave Newton Avatar answered Oct 26 '22 17:10

Dave Newton