JAVA用Calendar获取月、季度、半年 末日期
程序员文章站
2022-05-15 10:58:07
...
用java.util包下的Calendar 实现。
获取月末日期
Calendar calendar = Calendar.getInstance();
//如果要传日期过来 可以set一下
//calendar .setTime(date); ddate是你传过来的日期
calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
System.out.println(calendar.getTime()); //打印 月末最后一天
获取季度末的日期
Calendar cDay = Calendar.getInstance();
int curMonth = cDay.get(Calendar.MONTH); //获取当前月 int
if (curMonth >= Calendar.JANUARY && curMonth <= Calendar.MARCH){// >=0 <=2 一个季度
cDay.set(Calendar.MONTH, Calendar.MARCH);//set月为2
}
if (curMonth >= Calendar.APRIL && curMonth <= Calendar.JUNE){// >=3 <=5 一个季度
cDay.set(Calendar.MONTH, Calendar.JUNE);//set月为5
}
if (curMonth >= Calendar.JULY && curMonth <= Calendar.AUGUST) {// >=6 <=8 一个季度
cDay.set(Calendar.MONTH, Calendar.AUGUST);//set月为8
}
if (curMonth >= Calendar.OCTOBER && curMonth <= Calendar.DECEMBER) {// >=9 <=11 一个季度
cDay.set(Calendar.MONTH, Calendar.DECEMBER);//set月为11
}
cDay.set(Calendar.DAY_OF_MONTH, cDay.getActualMaximum(Calendar.DAY_OF_MONTH));//月末
//也可以设置小时分钟秒
//cDay.set(Calendar.HOUR,0);
//cDay.set(Calendar.MINUTE,0);
//cDay.set(Calendar.SECOND,0);
System.out.println(cDay.getTime());
获取半年的日期
Calendar cDay = Calendar.getInstance();
int curMonth = cDay.get(Calendar.MONTH);
if (curMonth >= Calendar.JANUARY && curMonth <= Calendar.JUNE){
cDay.set(Calendar.MONTH, Calendar.JUNE);
}
if (curMonth >= Calendar.JULY && curMonth <= Calendar.DECEMBER) {
cDay.set(Calendar.MONTH, Calendar.DECEMBER);
}
cDay.set(Calendar.DAY_OF_MONTH, cDay.getActualMaximum(Calendar.DAY_OF_MONTH));
//也可以设置小时分钟秒
//cDay.set(Calendar.HOUR,0);
//cDay.set(Calendar.MINUTE,0);
//cDay.set(Calendar.SECOND,0);
System.out.println(cDay.getTime());
上一篇: Java压缩解压缩文件
下一篇: 解压缩文件