LoginSignup
5
5

More than 5 years have passed since last update.

GroovyでDateの計算を楽にする

Last updated at Posted at 2014-04-01

TimeCategoryを使うと、Dateの計算が簡単にできる。
全て別のオブジェクトになるので、テストとかに使いやすい。

import groovy.time.*

use(TimeCategory) {
    def a = new Date("2014/4/1")
    12.times {
        println a + it.months    // 月を加算
    }
}

日や年、時間を計算することも可能

// 日を加算する場合
date + it.days
// 年を減算する場合
date - it.years
// 時を加算する場合
date + it.hours
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5