getYear() とか getMonth() とかないのかよ!
ということでUtilを作る。
public static int getYear() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
return calendar.get(Calendar.YEAR);
}
public static int getMonth() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
return calendar.get(Calendar.MONTH) + 1;
}
public static int getDay() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
return calendar.get(Calendar.DAY_OF_MONTH);
}
public static int getHour() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
return calendar.get(Calendar.HOUR_OF_DAY);
}
public static int getMinute() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
return calendar.get(Calendar.MINUTE);
}
public static int getSecond() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
return calendar.get(Calendar.SECOND);
}
よしできた。
getMonth() だけちょっと注意が必要で、
return calendar.get(Calendar.MONTH) + 1;
1月が 0 から始まるので、そこだけ意図的に1を足してます。