Calendar类
- Calendar提供了获取或设置各种日历字段的方法。
- 构造方法
- protected Calendar():由于修饰符是protected,所以无法直接创建该对象。
- 其他方法
import java.util.Calendar;
public class CalendarClass {
public static void main(String[] args) {
//1.创建Calendar对象
Calendar calendar= Calendar.getInstance();
System.out.println(calendar.getTime().toLocaleString());
System.out.println(calendar.getTimeInMillis());//获取从1970.1.1至今的毫秒值
//2.获取时间信息
//年
int year = calendar.get(Calendar.YEAR);
//月
int month = calendar.get(Calendar.MONTH);
//日
int day = calendar.get(Calendar.DATE);
//时
int hour = calendar.get(Calendar.HOUR_OF_DAY);//HOUR12小时 HOUR_OF_DAY24小时
//分
int minute = calendar.get(Calendar.MINUTE);
//秒
int second = calendar.get(Calendar.SECOND);
System.out.println(year+"年"+(month+1)+"月"+day+"日"+hour+"时"+minute+"分"+second+"秒");//月份返回值需要+1
//3.修改时间
Calendar calendar2 = Calendar.getInstance();
calendar2.set(Calendar.DATE,29);
System.out.println(calendar2.getTime().toLocaleString());
//4.add方法修改时间
calendar2.add(Calendar.HOUR,1);
System.out.println(calendar2.getTime().toLocaleString());
//5.补充方法
int MAX = calendar2.getActualMaximum(Calendar.DAY_OF_MONTH);
int MIN = calendar2.getActualMinimum(Calendar.DAY_OF_MONTH);
System.out.println(MAX);
System.out.println(MIN);
}
}
标签:int,calendar2,System,Calendar,calendar,out
From: https://www.cnblogs.com/cyyyds/p/17015725.html