Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incrementing date object by hours/minutes in Groovy

I want to add hours or minutes to a current date. For example I create a Date object with current time and date, and I want to increment it by 30min, how can I do such thing in Grails/Groovy ?

Date Now : Thu Jan 16 11:05:48 EST 2014
Adding 30min to Now : Thu Jan 16 11:35:48 EST 2014

I was wondering if I could do the same that we can do with add 1 to date and it moves it a day ahead.

like image 650
AlexCon Avatar asked Jan 16 '14 16:01

AlexCon


People also ask

How do I use Timecategory in groovy?

Method SummaryGet the DST offset (if any) for the default locale and the given date. Return a Duration representing the DST difference (if any) between two dates. Deprecated. Subtract one date from the other.

Which class in groovy specifies the instant date and time?

The class Date represents a specific instant in time, with millisecond precision. The Date class has two constructors as shown below.


1 Answers

You can use TimeCategory

import groovy.time.TimeCategory  currentDate =  new Date()  println currentDate  use( TimeCategory ) {     after30Mins = currentDate + 30.minutes }  println after30Mins 
like image 165
Rami Enbashi Avatar answered Oct 18 '22 19:10

Rami Enbashi