日付・時刻API(java.time)
日付・時刻API(java.time)
Section titled “日付・時刻API(java.time)”Java 8で導入されたjava.timeパッケージは、日付と時刻を扱うためのモダンなAPIです。この章では、java.timeの使い方について詳しく解説します。
java.timeパッケージの概要
Section titled “java.timeパッケージの概要”主なクラス:
LocalDate: 日付のみ(年、月、日)LocalTime: 時刻のみ(時、分、秒)LocalDateTime: 日付と時刻ZonedDateTime: タイムゾーン付きの日時Instant: エポック秒(UTC)Duration: 時間の間隔Period: 日付の間隔DateTimeFormatter: 日時のフォーマット
LocalDate
Section titled “LocalDate”日付のみを扱うクラスです。
// 現在の日付を取得LocalDate today = LocalDate.now();System.out.println(today); // 2024-01-15
// 特定の日付を指定LocalDate date1 = LocalDate.of(2024, 1, 15);LocalDate date2 = LocalDate.of(2024, Month.JANUARY, 15);
// 文字列から解析LocalDate date3 = LocalDate.parse("2024-01-15");
// カスタムフォーマットで解析DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");LocalDate date4 = LocalDate.parse("2024/01/15", formatter);
// 年、月、日の取得int year = today.getYear(); // 2024int month = today.getMonthValue(); // 1int day = today.getDayOfMonth(); // 15DayOfWeek dayOfWeek = today.getDayOfWeek(); // MONDAY
// 日付の計算LocalDate tomorrow = today.plusDays(1);LocalDate nextWeek = today.plusWeeks(1);LocalDate nextMonth = today.plusMonths(1);LocalDate nextYear = today.plusYears(1);
LocalDate yesterday = today.minusDays(1);LocalDate lastWeek = today.minusWeeks(1);
// 日付の比較boolean isAfter = today.isAfter(date1);boolean isBefore = today.isBefore(date1);boolean isEqual = today.isEqual(date1);
// うるう年の判定boolean isLeapYear = today.isLeapYear();LocalTime
Section titled “LocalTime”時刻のみを扱うクラスです。
// 現在の時刻を取得LocalTime now = LocalTime.now();System.out.println(now); // 14:30:45.123
// 特定の時刻を指定LocalTime time1 = LocalTime.of(14, 30);LocalTime time2 = LocalTime.of(14, 30, 45);LocalTime time3 = LocalTime.of(14, 30, 45, 123000000);
// 文字列から解析LocalTime time4 = LocalTime.parse("14:30:45");
// 時、分、秒の取得int hour = now.getHour(); // 14int minute = now.getMinute(); // 30int second = now.getSecond(); // 45int nano = now.getNano(); // 123000000
// 時刻の計算LocalTime plusHours = now.plusHours(2);LocalTime plusMinutes = now.plusMinutes(30);LocalTime plusSeconds = now.plusSeconds(10);
LocalTime minusHours = now.minusHours(1);
// 時刻の比較boolean isAfter = now.isAfter(time1);boolean isBefore = now.isBefore(time1);LocalDateTime
Section titled “LocalDateTime”日付と時刻を扱うクラスです。
// 現在の日時を取得LocalDateTime now = LocalDateTime.now();System.out.println(now); // 2024-01-15T14:30:45.123
// 特定の日時を指定LocalDateTime dateTime1 = LocalDateTime.of(2024, 1, 15, 14, 30);LocalDateTime dateTime2 = LocalDateTime.of(2024, Month.JANUARY, 15, 14, 30, 45);
// LocalDateとLocalTimeから作成LocalDate date = LocalDate.of(2024, 1, 15);LocalTime time = LocalTime.of(14, 30);LocalDateTime dateTime3 = LocalDateTime.of(date, time);
// 文字列から解析LocalDateTime dateTime4 = LocalDateTime.parse("2024-01-15T14:30:45");
// 日時の計算LocalDateTime tomorrow = now.plusDays(1);LocalDateTime nextHour = now.plusHours(1);LocalDateTime nextMinute = now.plusMinutes(30);
// LocalDateとLocalTimeに変換LocalDate datePart = now.toLocalDate();LocalTime timePart = now.toLocalTime();ZonedDateTime
Section titled “ZonedDateTime”タイムゾーン付きの日時を扱うクラスです。
// 現在の日時(システムのタイムゾーン)ZonedDateTime now = ZonedDateTime.now();System.out.println(now); // 2024-01-15T14:30:45.123+09:00[Asia/Tokyo]
// 特定のタイムゾーンを指定ZonedDateTime tokyo = ZonedDateTime.now(ZoneId.of("Asia/Tokyo"));ZonedDateTime newYork = ZonedDateTime.now(ZoneId.of("America/New_York"));ZonedDateTime london = ZonedDateTime.now(ZoneId.of("Europe/London"));
// LocalDateTimeとZoneIdから作成LocalDateTime localDateTime = LocalDateTime.of(2024, 1, 15, 14, 30);ZonedDateTime zoned = ZonedDateTime.of(localDateTime, ZoneId.of("Asia/Tokyo"));
// タイムゾーンの変換ZonedDateTime tokyoTime = ZonedDateTime.now(ZoneId.of("Asia/Tokyo"));ZonedDateTime newYorkTime = tokyoTime.withZoneSameInstant(ZoneId.of("America/New_York"));
// タイムゾーンの取得ZoneId zone = now.getZone();System.out.println(zone); // Asia/TokyoInstant
Section titled “Instant”エポック秒(1970-01-01T00:00:00Zからの経過秒数)を扱うクラスです。
// 現在のエポック秒を取得Instant now = Instant.now();System.out.println(now); // 2024-01-15T05:30:45.123Z
// エポック秒から作成Instant instant1 = Instant.ofEpochSecond(1705289445);Instant instant2 = Instant.ofEpochMilli(1705289445123L);
// LocalDateTimeと相互変換LocalDateTime localDateTime = LocalDateTime.of(2024, 1, 15, 14, 30);Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
LocalDateTime converted = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
// エポック秒の取得long epochSecond = now.getEpochSecond();long epochMilli = now.toEpochMilli();Duration
Section titled “Duration”時間の間隔を扱うクラスです(秒、ナノ秒単位)。
// Durationの作成Duration duration1 = Duration.ofHours(2);Duration duration2 = Duration.ofMinutes(30);Duration duration3 = Duration.ofSeconds(45);Duration duration4 = Duration.parse("PT2H30M45S"); // 2時間30分45秒
// LocalTime間のDurationLocalTime start = LocalTime.of(9, 0);LocalTime end = LocalTime.of(17, 30);Duration workDuration = Duration.between(start, end);System.out.println(workDuration); // PT8H30M
// Instant間のDurationInstant startInstant = Instant.now();Thread.sleep(1000);Instant endInstant = Instant.now();Duration elapsed = Duration.between(startInstant, endInstant);
// Durationの計算Duration plus = duration1.plus(duration2);Duration minus = duration1.minus(duration2);
// 時間の取得long hours = workDuration.toHours(); // 8long minutes = workDuration.toMinutes(); // 510long seconds = workDuration.getSeconds(); // 30600Period
Section titled “Period”日付の間隔を扱うクラスです(年、月、日単位)。
// Periodの作成Period period1 = Period.ofYears(1);Period period2 = Period.ofMonths(6);Period period3 = Period.ofDays(30);Period period4 = Period.parse("P1Y6M30D"); // 1年6ヶ月30日
// LocalDate間のPeriodLocalDate start = LocalDate.of(2023, 1, 15);LocalDate end = LocalDate.of(2024, 7, 15);Period period = Period.between(start, end);System.out.println(period); // P1Y6M
// Periodの計算Period plus = period1.plus(period2);Period minus = period1.minus(period2);
// 期間の取得int years = period.getYears(); // 1int months = period.getMonths(); // 6int days = period.getDays(); // 0DateTimeFormatter
Section titled “DateTimeFormatter”日時のフォーマットとパースを行うクラスです。
// 標準フォーマッターLocalDateTime now = LocalDateTime.now();
DateTimeFormatter isoFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;String formatted1 = now.format(isoFormatter);LocalDateTime parsed1 = LocalDateTime.parse(formatted1, isoFormatter);
// カスタムフォーマッターDateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");String formatted2 = now.format(customFormatter);LocalDateTime parsed2 = LocalDateTime.parse(formatted2, customFormatter);
// ロケールを指定DateTimeFormatter japaneseFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日", Locale.JAPANESE);String formatted3 = now.format(japaneseFormatter);
// よく使うパターンDateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");ユーザーの生年月日から年齢を計算
Section titled “ユーザーの生年月日から年齢を計算”public class UserService {
public int calculateAge(LocalDate birthDate) { LocalDate today = LocalDate.now(); Period period = Period.between(birthDate, today); return period.getYears(); }
public boolean isAdult(LocalDate birthDate) { return calculateAge(birthDate) >= 18; }
public long daysUntilBirthday(LocalDate birthDate) { LocalDate today = LocalDate.now(); LocalDate nextBirthday = birthDate.withYear(today.getYear());
if (nextBirthday.isBefore(today) || nextBirthday.isEqual(today)) { nextBirthday = nextBirthday.plusYears(1); }
return ChronoUnit.DAYS.between(today, nextBirthday); }}営業時間のチェック
Section titled “営業時間のチェック”public class BusinessHoursChecker {
private static final LocalTime OPEN_TIME = LocalTime.of(9, 0); private static final LocalTime CLOSE_TIME = LocalTime.of(18, 0);
public boolean isBusinessHours(LocalDateTime dateTime) { LocalTime time = dateTime.toLocalTime(); DayOfWeek dayOfWeek = dateTime.getDayOfWeek();
// 平日のみ if (dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY) { return false; }
return !time.isBefore(OPEN_TIME) && time.isBefore(CLOSE_TIME); }
public Duration getRemainingBusinessHours(LocalDateTime dateTime) { if (!isBusinessHours(dateTime)) { return Duration.ZERO; }
LocalTime time = dateTime.toLocalTime(); LocalTime endOfDay = LocalTime.of(18, 0); return Duration.between(time, endOfDay); }}日付・時刻APIのポイント:
- LocalDate: 日付のみ
- LocalTime: 時刻のみ
- LocalDateTime: 日付と時刻
- ZonedDateTime: タイムゾーン付き日時
- Instant: エポック秒
- Duration: 時間の間隔
- Period: 日付の間隔
- DateTimeFormatter: フォーマットとパース
java.timeパッケージを使用することで、日付と時刻を安全かつ効率的に扱えます。