Skip to content

Instantly share code, notes, and snippets.

@kimukou
Created March 29, 2011 09:14
Show Gist options
  • Save kimukou/892059 to your computer and use it in GitHub Desktop.
Save kimukou/892059 to your computer and use it in GitHub Desktop.
Joda-Time-test.groovy
//
// http://www.ibm.com/developerworks/jp/java/library/j-jodatime.html
// http://program.g.hatena.ne.jp/halflite/20101120/seventeen
// http://d.hatena.ne.jp/ikeike443/20100406/1270572212
//
@Grab(group='joda-time', module='joda-time', version='1.6.2')
import org.joda.time.DateTime
import org.joda.time.Days
import org.joda.time.Period
import org.joda.time.PeriodType
import org.joda.time.LocalDate
import org.joda.time.DateMidnight
import org.joda.time.format.DateTimeFormat
dt = new DateTime("2010-04-05T13:20:25")
dt2 = new DateTime("2010-04-05") //これでももちろんいい
println dt
println dt2
//今月の末日を取得したい場合
dt = new DateTime().dayOfMonth().withMaximumValue()
//指定された日の90日後の週の最初の日を取得したい場合
dt2 = new DateTime("2010-04-05").plusDays(90).dayOfWeek().withMinimumValue()
println dt
println dt2
//指定された月(下記では2010年4月)の末日を取得し、特定の書式にフォーマットした文字列を作りたい場合
String dtstr = new DateTime(2010,04,1,0,0,0,0).dayOfMonth().withMaximumValue().toString("yyyy年MM月dd日")
println dtstr
// 誰かの誕生日
String birthDay = "1964/09/25"
// 誕生日の文字列をDateTime型に変換
DateTime birthDateTime = DateTimeFormat.forPattern("yyyy/MM/dd").parseDateTime(birthDay)
// 17歳の誕生日
DateTime seventeenthBirthDateTime = birthDateTime.plusYears(17)
// なう
DateTime now = new DateTime()
// 出力
println now.toString("本日 yyyy年MM月dd日")
// 期間タイプは、月日(年月日から年を除いたもの)
PeriodType periodType = PeriodType.yearMonthDay().withYearsRemoved()
// 期間を求める
Period period = new Period(seventeenthBirthDateTime, now, periodType)
// 経過月日を取得
int months = period.getMonths()
int days = period.getDays()
String beforeAfter = (months < 0 || days < 0) ? "まで" : "と"
// 出力
println "17歳$beforeAfter${Math.abs(months)}ヶ月${Math.abs(days)}日"
// 経過日数を求める
int days2 = Days.daysBetween(seventeenthBirthDateTime, now).getDays()
String beforeAfter2 = (days2 < 0) ? "まで" : "と"
// 出力
println "17歳$beforeAfter2${Math.abs(days2)}日"
/*
//SystemFactoryが最新版jarに無いので動かない?
// javadoc http://joda-time.sourceforge.net/api-release/index.html
//リスト 8. Joda の DateTime クラスから JDK クラスを作成する
DateTime dateTime = SystemFactory.getClock().getDateTime()
Calendar calendar = dateTime.toCalendar(Locale.getDefault())
date = dateTime.toDate()
println date
DateMidnight dateMidnight = SystemFactory.getClock().getDateMidnight()
date = dateMidnight.toDate()
println date
//リスト 9. LocalDate を表す Date オブジェクトを作成する
LocalDate localDate = SystemFactory.getClock().getLocalDate()
Date date = localDate.toDateMidnight().toDate()
println date
//リスト 10. ISO-8601 を使用する
dateTime = SystemFactory.getClock().getDateTime()
println dateTime.toString(ISODateTimeFormat.basicDateTime())
println dateTime.toString(ISODateTimeFormat.basicDateTimeNoMillis())
println dateTime.toString(ISODateTimeFormat.basicOrdinalDateTime())
println dateTime.toString(ISODateTimeFormat.basicWeekDateTime())
//リスト 11. SimpleDateFormat 文字列を渡す
DateTime dateTime = SystemFactory.getClock().getDateTime()
println dateTime.toString("MM/dd/yyyy hh:mm:ss.SSSa")
println dateTime.toString("dd-MM-yyyy HH:mm:ss")
println dateTime.toString("EEEE dd MMMM, yyyy HH:mm:ssa")
println dateTime.toString("MM/dd/yyyy HH:mm ZZZZ")
println dateTime.toString("MM/dd/yyyy HH:mm Z")
*/
// Joda-Timeで期間を扱う
// http://seacolorswind.sakura.ne.jp/blog/2010/12/joda-time.html
//
import org.joda.time.DateTime
import org.joda.time.Duration
import org.joda.time.Interval
import org.joda.time.Period
DateTime startDT = new DateTime()
DateTime endDT = startDT.plusDays(1).plusMillis(1)
Interval interval = new Interval(startDT, endDT)
Duration duration = interval.toDuration()
println "duration: " + duration.getMillis()
period = interval.toPeriod()
println "period: " + period.getMillis()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment