Calendar calendar = Calendar.getInstance();
Date date = calendar2.getTime();
public static void CalenderTest() {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int date = calendar.get(Calendar.DATE);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
System.out.println(year + "年" + month + "月" + date + "日" + hour + "时" + minute + "分" + second + "秒");
}
Calendar修改时间有两个方法:set方法和add方法
set与add对比:
1.set是将Calendar对象直接设置为指定的时间,add方法是在Calendar对象当前时间的基础上增加减少指定的时间
2.add方法第二个参数为正则增加时间,为负则是减少时间,set方法第二个参数不能是负数
public static void CalendarSetGet() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
calendar.set(Calendar.YEAR, 2023);
System.out.println("将日期的年设置为2012年:"+","+dateFormat.format(calendar.getTime()));
//0-11为本年,-1往下为前一年,12往上为下一年
calendar.set(Calendar.MONTH, 8);
System.out.println("将日期的月设置为8月:"+","+dateFormat.format(calendar.getTime()));
//参数对应月的天数,1-30或者1-31,小于这个范围为上个月,大于这个范围为下个月
calendar.set(Calendar.DATE, 15);
System.out.println("将日期的日设置为15日:"+","+dateFormat.format(calendar.getTime()));
//与Calendar.DATE一样
calendar.set(Calendar.DAY_OF_MONTH, 1);
//12小时制,实际测试参数超过12会自动变为24小时制,同样的参数小于1为昨天,大于24为明天
calendar.set(Calendar.HOUR, 6);
System.out.println("将日期的时设置为6时:"+","+dateFormat.format(calendar.getTime()));
calendar.set(Calendar.HOUR_OF_DAY, 11); //24小时制
//下面的分钟和秒与上面的规则一样
calendar.set(Calendar.MINUTE, 45);
calendar.set(Calendar.SECOND, 26);
//设置时间为本周的第几天,参数不管是多少,获取的日期始终在本周的7天内循环,2,9,16,23....是周一
calendar.set(Calendar.DAY_OF_WEEK, 3);
System.out.println("将日期的周设置为第3天:"+","+dateFormat.format(calendar.getTime()));
//设置时间的年月日
calendar.set(2015, 5, 16);
System.out.println(dateFormat.format(calendar.getTime()));
//设置时间的年月日时分
calendar.set(2015, 5, 16, 8, 30);
System.out.println(dateFormat.format(calendar.getTime()));
}
Calendar calendar2 = Calendar.getInstance();
calendar2.add(Calendar.YEAR,2);
System.out.println("将日期的年增加2年:"+","+dateFormat.format(calendar2.getTime()));
calendar2.add(Calendar.MONTH,2);
System.out.println("将日期的月增加2个月:"+","+dateFormat.format(calendar2.getTime()));
calendar2.add(Calendar.DATE,2);
System.out.println("将日期的天增加2天:"+","+dateFormat.format(calendar2.getTime()));
//DAY_OF_MONTH和DAY_OF_WEEK与DATE意思相同
calendar2.add(Calendar.DAY_OF_MONTH,2);
calendar2.add(Calendar.DAY_OF_WEEK,2);
calendar2.add(Calendar.MONTH,-2);
System.out.println("将日期的月减少2个月:"+","+dateFormat.format(calendar2.getTime()));
calendar2.add(Calendar.DATE,-2);
System.out.println("将日期的天减少2天:"+","+dateFormat.format(calendar2.getTime()));
//获取指定日期的当月的第一天和最后一天
public static void getMonthDay() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//将日期修改为当月1号
calendar.set(Calendar.DATE, 1);
String data_start = dateFormat.format(calendar.getTime());
//将日期的月份+1变为下个月
calendar.add(Calendar.MONTH, 1);
//将日期修改为当月的0号,也就是上个月的最后一天
calendar.set(Calendar.DATE, 0);
String data_end = dateFormat.format(calendar.getTime());
System.out.println(data_start+","+data_end);
}
说明:
public static void getWeekDay() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//将日期改为本周的第二天,也就是实际时间的本周第一天
calendar.set(Calendar.DAY_OF_WEEK, 2);
String data_start = dateFormat.format(calendar.getTime());
//将日期增加七天变为下周
calendar.add(Calendar.DAY_OF_WEEK, 7);
//将日期设置为本周第一天,也就是上周的最后一天
calendar.set(Calendar.DAY_OF_WEEK, 1);
String data_end = dateFormat.format(calendar.getTime());
System.out.println(data_start+","+data_end);
}
public static void getWeekDay2() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
calendar.add(Calendar.DAY_OF_WEEK, -1);
}
Date[] dates = new Date[7]; // new Date[7] 星期日
for (int i = 0; i < 7; i++) { // i < 7 星期日
dates[i] = calendar.getTime();
calendar.add(Calendar.DATE, 1);
}
//for循环执行完后,dates这个数组里面放的就是本周所有的日期
String data_start = dateFormat.format(dates[0]);
String data_end = dateFormat.format(dates[6]);
System.out.println(data_start+","+data_end);
}
Calendar类还有很多方法这里没有讲解,包括Calendar的实现GregorianCalendar类,以后用到的的时候再研究