Last active
December 12, 2015 01:38
-
-
Save koji-k/4692563 to your computer and use it in GitHub Desktop.
This is usage of Date object on Groovy2.1
Groovy2.1でのDateオブジェクトの使い方です。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ******************************************* | |
// This is usage of Date object on Groovy2.1 | |
// Groovy2.1でのDateオブジェクトの使い方です。 | |
// ******************************************* | |
// Generate Date object.(Default TimeZone is "CET" in my computer) | |
// Dateオブジェクトの生成(私の環境ではデフォルトのタイムゾーンは"CET") | |
// sample of how to declare | |
// def sample = new Date() | |
// def sample = Date.parse("yyyy/MM/dd HH:mm:ss", "1985/02/20 12:34:56") | |
// def sample = new Date("1985/02/20 12:34:56") | |
def today = new Date("2013/02/01 12:34:56") | |
assert today.format("yyyy/MM/dd HH:mm:ss z") == "2013/02/01 12:34:56 CET" | |
// change the TimeZone from CET to Japan when the display. | |
// 表示時にタイムゾーンをCETからJapanに変更 | |
assert today.format("yyyy/MM/dd HH:mm:ss z", TimeZone.getTimeZone("Japan")) == "2013/02/01 20:34:56 JST" | |
// change the date and time in Date object.(future) | |
// I used "clone()" for assert for check variable. of course you can use "new Date()" too. | |
// Dateオブジェクトの日時を変更(future) | |
// assertで中身を確認する為に"clone()"を使いました。もちろん"new Date()"でもかまいません。 | |
def future = today.clone() | |
assert future.format("yyyy/MM/dd HH:mm:ss z") == "2013/02/01 12:34:56 CET" | |
use(groovy.time.TimeCategory){ | |
future = future + 2.year | |
future = future + 2.month | |
future = future + 2.day | |
future = future + 2.hour | |
future = future + 2.minute | |
future = future + 2.second | |
} | |
// calculated date and time in Date object.(future) | |
// Achtung! | |
// Why has changed to "CEST" timezone? | |
// Because this future date and time is summer time. | |
// Dateオブジェクトの日時の計算結果(future) | |
assert future.format("yyyy/MM/dd HH:mm:ss z") == "2015/04/03 14:36:58 CEST" | |
// difference of today and future.(Number of days) | |
// todayとfutureの差分(日数) | |
assert future.minus(today) == 791 | |
// difference of today and future.(Number of second) | |
// todayとfutureの差分(秒数) | |
def msec = future.toTimestamp().getTime() - today.toTimestamp().getTime() | |
assert msec / 1000 == 68346122 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment