创建对象
默认创建的对象的时间是当前时间
Calendar instance = Calendar.getInstance();
常用方法
get、set、add
set方法设置时间
get方法获取时间
add对当前时间进行加减操作
Calendar instance = Calendar.getInstance();
//Wed Jul 06 10:12:41 CST 2022
System.out.println(instance.getTime());
instance.set(Calendar.YEAR,1998);
//后面参数0-11对应 1-12 12个月份
instance.set(Calendar.MONTH,7);
instance.set(Calendar.DATE,23);
instance.set(Calendar.HOUR_OF_DAY, 22);
instance.set(Calendar.MINUTE, 0);
instance.set(Calendar.SECOND, 0);
instance.set(Calendar.MILLISECOND, 0);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//1998-08-23 22:00:00
System.out.println(simpleDateFormat.format(instance.getTime()));
instance.add(Calendar.DAY_OF_YEAR, -2);
//1998-08-21 22:00:00
System.out.println(simpleDateFormat.format(instance.getTime()));
版权声明:本文为qq_45092920原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。