在 Groovy 中按小时/分钟递增日期对象 [英] Incrementing date object by hours/minutes in Groovy

查看:25
本文介绍了在 Groovy 中按小时/分钟递增日期对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为当前日期添加小时或分钟.例如,我创建了一个带有当前时间和日期的 Date 对象,我想将它增加 30 分钟,我该如何在 Grails/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 ?

现在日期:美国东部时间 2014 年 1 月 16 日星期四 11:05:48
现在增加 30 分钟:美国东部时间 2014 年 1 月 16 日星期四 11:35:48

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

我想知道我是否可以做同样的事情,我们可以将日期加 1 并将其提前一天.

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.

推荐答案

可以使用 TimeCategory

You can use TimeCategory

import groovy.time.TimeCategory

currentDate =  new Date()

println currentDate

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

println after30Mins

这篇关于在 Groovy 中按小时/分钟递增日期对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆