Posted By

laurenceosx on 05/11/11


Tagged


Versions (?)

Groovy startOfDay endOfDay nextDayStart


 / Published in: Groovy
 

  1. // TODO check to see if calcs are correct for spring and fall DST changes.
  2.  
  3. def startOfDay( java.util.Date aDate ) {
  4. final long MS_PER_DAY = (24 * 60 * 60 * 1000);
  5. long tzOffset = java.util.TimeZone.getDefault().getOffset( aDate.time );
  6. long msSinceMidnight = aDate.time % MS_PER_DAY;
  7. return new java.util.Date( aDate.time - msSinceMidnight - tzOffset );
  8. }
  9.  
  10. def endOfDay( java.util.Date aDate ) {
  11. final long MS_PER_DAY = (24 * 60 * 60 * 1000);
  12. def long startMs = startOfDay( aDate ).time;
  13. def long endMs = startMs + MS_PER_DAY - 1l;
  14. return new java.util.Date( endMs );
  15. }
  16.  
  17. def nextDayStart( java.util.Date aDate ) {
  18. final long MS_PER_DAY = (24 * 60 * 60 * 1000);
  19. return new java.util.Date( (startOfDay(aDate).time + MS_PER_DAY) );
  20. }

Report this snippet  

You need to login to post a comment.