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で「日時に関する情報を取得する」の動作を確認してみました。以下のページを参考にしました。

実装

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

TestCalendar1.java
import java.util.Calendar;

class TestCalendar1{
  public static void main(String args[]){
    String[] week_name = {"日曜日", "月曜日", "火曜日", "水曜日", 
                          "木曜日", "金曜日", "土曜日"};

    Calendar calendar = Calendar.getInstance();

    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH) + 1;
    int day = calendar.get(Calendar.DATE);
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);
    int second = calendar.get(Calendar.SECOND);
    int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;

    int day_of_year = calendar.get(Calendar.DAY_OF_YEAR);

    System.out.println("現在の日時は");
    System.out.println(year + "年" + month + "月" + day + "日");
    System.out.println("(" + week_name[week] + ")");
    System.out.println(hour + "時" + minute + "分" + second + "秒");

    System.out.println("今日は今年の" + day_of_year + "日目です");
  }
}

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

$ javac TestCalendar1.java 
$ java TestCalendar1 
現在の日時は
2023年9月24日
(日曜日)
9時16分58秒
今日は今年の267日目です

まとめ

何かの役に立てばと。

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?