Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

datetime - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use TimeCategory

import groovy.time.TimeCategory

currentDate =  new Date()

println currentDate

use( TimeCategory ) {
    after30Mins = currentDate + 30.minutes
}

println after30Mins

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...