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 3 years have passed since last update.

java Calendarクラス(時刻のセット、比較、フォーマット)

Posted at

#Calendarクラスのインスタンスに任意の時刻をセット


Calendar tempTime = Calendar.getInstance();
            
 // 時間をセット
 tempTime.set(Calendar.HOUR_OF_DAY, trainHour);
 tempTime.set(Calendar.MINUTE, trainTime);

#AがBより前の時間かどうか比較


boolean isBefore = false;
//tempTimeがsafeTimeより前の時間かどうか
 isBefore  = tempTime.before(safeTime);

#「HH:mm」にフォーマットして出力

import java.text.SimpleDateFormat;

---------------------------------------

// カレンダークラスのインスタンスを生成
Calendar ansTime = Calendar.getInstance();

Date time = ansTime.getTime();
        
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

System.out.println(sdf.format(time));
0
0
2

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?