package test;
import java.util.ArrayList;
import java.util.List;
import org.joda.time.DateMidnight;
import org.joda.time.DateTime;
import org.joda.time.Interval;
import org.junit.Test;
public class JodaTimeSample {
@Test
public void sample1() {
// 現在日時
DateTime datetime = new DateTime();
System.out.println("現在の日時: " + datetime);
System.out.println("年: " + datetime.getYear());
System.out.println("月: " + datetime.getMonthOfYear());
System.out.println("日: " + datetime.getDayOfMonth());
System.out.println("曜日: " + datetime.getDayOfWeek());
System.out.println("時: " + datetime.getHourOfDay());
System.out.println("分: " + datetime.getMinuteOfHour());
System.out.println("秒: " + datetime.getSecondOfMinute());
// 2005年の今日
System.out.println("2005年の今日: " + datetime.withYear(2005));
// 一ヶ月後
System.out.println("1ヶ月後: " + datetime.plusMonths(1));
// 文字列フォーマットと日付型変換
DateTime dt1 = new DateTime("2011-04-05T13:20:25");
System.out.println(dt1.toString("yyyy年MM月dd日"));
System.out.println(dt1.toDate());
DateTime dt2 = new DateTime("2011-04-05");
System.out.println(dt2.toString("yyyy年MM月dd日"));
System.out.println(dt2.toDate());
// これはダメ
DateTime dt3 = new DateTime("20110405");
System.out.println(dt3.toString("yyyy年MM月dd日"));
System.out.println(dt3.toDate());
}
@Test
public void sample2() {
DateMidnight dm1 = new DateMidnight(2011, 6, 17);
DateTime dm2 = new DateTime(2011, 6, 19, 23, 59, 59, 0);
// 日付範囲を持つオブジェクト
Interval interval = new Interval(dm1, dm2);
System.out.println(interval.getStart());
System.out.println(interval.getEnd());
}
@Test
public void sample3() {
// 日曜日始まりの週表示のカレンダー
int y = 2011;
int m = 6;
// 月の初日の前週の日曜日を取得
DateMidnight str = new DateMidnight(y, m, 1).withDayOfWeek(1).minusDays(1);
// 月の末日の週の土曜日を取得
DateMidnight end = new DateMidnight(y, m, 1).dayOfMonth().withMaximumValue().withDayOfWeek(7);
// ネスト構造のリストにする
List<List<String>> month = new ArrayList<List<String>>();
List<String> week = new ArrayList<String>();
while (!str.equals(end)) {
week.add(String.format("%02d", str.getDayOfMonth()));
str = str.plusDays(1);
if (str.getDayOfWeek() == 7) {
month.add(week);
week = new ArrayList<String>();
}
}
// 表示
System.out.println(String.format(" %s年%s月", y, m));
System.out.println(" 日 月 火 水 木 金 土");
for (List<String> w : month) {
for (String d : w) {
System.out.print(" " + d);
}
System.out.println("");
}
}
@Test
public void sample4() {
// 日付の範囲を取得
DateTime str = new DateTime(2011, 1, 1, 12, 0, 0, 0);
DateTime end = new DateTime(2011, 1, 2, 13, 30, 0, 0);
Interval interval = new Interval(str, end);
Period period = interval.toPeriod();
System.out.println(period.getDays());
System.out.println(period.getHours());
System.out.println(period.getMinutes());
}
@Test
public void sample5() {
DateTime str1 = new DateTime(0, 1, 1, 0, 0, 0, 0);
DateTime end1 = new DateTime(2011, 6, 1, 23, 59, 59, 0);
DateTime str2 = new DateTime(2011, 6, 2, 0, 0, 0, 0);
DateTime end2 = new DateTime(2011, 6, 3, 23, 59, 59, 0);
DateTime str3 = new DateTime(2011, 6, 4, 0, 0, 0, 0);
DateTime end3 = new DateTime(9999, 1, 1, 0, 0, 0, 0);
Interval interval1 = new Interval(str1, end1);
Interval interval2 = new Interval(str2, end2);
Interval interval3 = new Interval(str3, end3);
interval1.withEnd(end2);
List<Interval> list = new ArrayList<Interval>();
list.add(interval1);
list.add(interval2);
list.add(interval3);
// 開始日・終了日を一日プラスしてみる
List<Interval> list2 = new ArrayList<Interval>();
for(Interval i : list){
list2.add(i.withEnd(i.getEnd().plusDays(1)).withStart(i.getStart().plusDays(1)));
}
for (Interval i : list2) {
if(i.getStart().getYear() == 0){
System.out.println(i.getEnd().toString("yyyy/MM/dd"));
}else if(i.getEnd().getYear() == 9999){
System.out.println(i.getStart().toString("yyyy/MM/dd"));
}else if(i.getStart().toString("yyyy/MM/dd").equals(i.getEnd().toString("yyyy/MM/dd"))){
System.out.println(i.getStart().toString("yyyyMMdd"));
}else{
System.out.println(i.getStart().toString("yyyy/MM/dd") + "~" + i.getEnd().toString("yyyy/MM/dd"));
}
}
}
}
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme