JAva Calendarによる指定時間の取得

1973 ワード

例1:今日の0時0分0秒を取得
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,0);
calendar.set(Calendar.MINUTE,0);
calendar.set(Calendar.SECOND,0);
Date today = calendar.getTime();

例2:先月の初日の1秒目を取得する
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY,0);
calendar.set(Calendar.MINUTE,0);
calendar.set(Calendar.SECOND,0);
Date startDate = calendar.getTime();