0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Javaで「日時を比較する」の動作を確認してみた

Posted at

概要

Javaで「日時を比較する」の動作を確認してみました。以下のページを参考にしました。

実装

以下のファイルを作成しました。

TestCalendar4.java
import java.util.Calendar;

class TestCalendar4{
  public static void main(String args[]){

    Calendar calendar1 = Calendar.getInstance();

    System.out.println("日時1を2006/05/08 09:45:10に設定します");
    calendar1.set(2006, 4, 8, 9, 45, 10);

    Calendar calendar2 = Calendar.getInstance();

    System.out.println("日時2を2006/05/11 12:58:15に設定します");
    calendar2.set(2006, 4, 11, 12, 58, 15);

    int diff = calendar1.compareTo(calendar2);

    if (diff == 0){
      System.out.println("日付1と日付2は同じ日時です");
    }else if (diff > 0){
      System.out.println("日付1は日付2より進んでいます");
    }else{
      System.out.println("日付1は日付2より遅れています");
    }
  }
}

以下のコマンドを実行しました。

$ javac TestCalendar4.java
$ java TestCalendar4 
日時1を2006/05/08 09:45:10に設定します
日時2を2006/05/11 12:58:15に設定します
日付1は日付2より遅れています

まとめ

何かの役に立てばと。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?