LoginSignup
7
3

More than 5 years have passed since last update.

JAVA getTime() setTime(変数)

Last updated at Posted at 2015-11-11

■getTime()
1970年1月1日午前0時からの経過時間をミリ秒で返します

 DateクラスでのgetTime()
import java.util.Date;

public class Test02 {
    public static void main(String[] args) {
        Date now = new Date();
        System.out.println(now);
        System.out.println(now.getTime());
    }
}

実行結果
Wed Nov 11 10:33:57 JST 2015
1447205637038
 CalendarクラスでのgetTime()
import java.util.Calendar;

public class Test02 {
    public static void main(String[] args) {
        Calendar a = Calendar.getInstance();
        System.out.println(a.getTime());
    }
}

実行結果
Wed Nov 11 10:45:46 JST 2015

■補足___________________________
■CalendarクラスでのgetTime()
現在の日時を返します

■DateクラスでのgetTime()
1970年1月1日0時0分0秒らかの経過時間をミリ秒単位の値で取得できます。処理開始直前と直後に経過時間を調べることによって、処理にかかった時間が得られます。


■setTime(変数)
Dateオブジェクトに、引数で指定された1970年1月1日午前0時からの経過時間(ミリ秒)の日時を指定する

■Date型の値をCalendar型に変換する

        Date now = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(now);

dateに何か値が入っている場合は上記のようにCalendar型の変数を用意して、 そこにsetTimeでdateの値を代入しcalendar型への変換をすることが出来ます。

7
3
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
7
3