Javaの日時処理クラス

Javaにおける日時処理にはいくつかの重要なクラスが用いられます。ここでは、DateSimpleDateFormatCalendar、およびZoneIdクラスについて説明します。

Dateクラス

Dateクラスは、特定の瞬間を表すために使用され、java.utilパッケージに含まれています。

  • 現在の日時を取得する。
  • コンストラクタを通じて、現在の日時や特定の時間(1970年1月1日からのミリ秒数)を指定してオブジェクトを作成する。
  • 複雑な日時操作は推奨されず、代わりにCalendarクラスを使用することをお勧めします。
  • 日時を文字列にフォーマットしたり、文字列から日時を解析したりするためには、DateFormatクラスとそのサブクラスであるSimpleDateFormatを使用します。
  • 国際化に対応していないため、多言語環境や異なるタイムゾーンを扱う場合は、他のクラスやメソッドを使用することを検討してください。
import java.util.Date;

public class CurrentDateTime {
    public static void main(String[] args) {
        // 現在の日時を取得
        Date now = new Date();
        System.out.println("現在の日時: " + now);

        // 特定のミリ秒数から日時を作成
        long timeInMillis = 1625497600000L; // 2021年7月1日
        Date specificTime = new Date(timeInMillis);
        System.out.println("指定された日時: " + specificTime);
    }
}

SimpleDateFormatクラス

SimpleDateFormatクラスは、日時をフォーマットしたり解析したりするためのクラスで、java.textパッケージに含まれています。

  • 日時を指定された形式の文字列に変換する。
  • 指定された形式の文字列から日時を解析する。
  • パターン文字を使って日時の形式をカスタマイズする。
  • Localeクラスと組み合わせることで、異なる言語環境での日時フォーマットを実現する。
  • スレッドセーフではありませんので、多スレッド環境では注意が必要です。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTimeFormatter {
    public static void main(String[] args) {
        // 日時フォーマッタを作成
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 現在の日時をフォーマット
        Date now = new Date();
        String formattedNow = formatter.format(now);
        System.out.println("フォーマット後の日時: " + formattedNow);

        // 文字列から日時を解析
        String dateTimeString = "2022-01-01 12:00:00";
        try {
            Date parsedDate = formatter.parse(dateTimeString);
            System.out.println("解析後の日時: " + parsedDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

Calendarクラス

Calendarクラスは、日時を操作するために使用される抽象クラスで、java.utilパッケージに含まれています。

  • 現在の日時を取得する。
  • 年、月、日などのカレンダーフィールドの値を取得または設定する。
  • 日時計算を行う。
import java.util.Calendar;

public class CalendarOperations {
    public static void main(String[] args) {
        // 現在の日時を取得
        Calendar calendar = Calendar.getInstance();
        System.out.println("現在の日時: " + calendar.getTime());

        // カレンダーフィールドの操作
        int currentYear = calendar.get(Calendar.YEAR);
        int currentMonth = calendar.get(Calendar.MONTH) + 1; // 0始まりなので調整
        int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
        int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
        int currentMinute = calendar.get(Calendar.MINUTE);
        int currentSecond = calendar.get(Calendar.SECOND);
        System.out.println("現在の日付: " + currentYear + "-" + currentMonth + "-" + currentDay);
        System.out.println("現在の時間: " + currentHour + ":" + currentMinute + ":" + currentSecond);

        // 日時計算
        calendar.add(Calendar.DATE, 5); // 5日後
        System.out.println("5日後: " + calendar.getTime());
        calendar.add(Calendar.MONTH, -2); // 2ヶ月前
        System.out.println("2ヶ月前: " + calendar.getTime());
        calendar.add(Calendar.YEAR, 1); // 1年後
        System.out.println("1年後: " + calendar.getTime());
    }
}

ZoneIdクラス

ZoneIdクラスは、タイムゾーンを扱うためのクラスで、java.timeパッケージに含まれています。

  • タイムゾーンIDに基づいてZoneIdオブジェクトを作成する。
  • 利用可能なすべてのタイムゾーンIDの一覧を取得する。

タグ: Date SimpleDateFormat Calendar ZoneId java.util

6月23日 20:32 投稿