//计算天数
public List day(String dates, String datee) throws ParseException {
List dayls = new ArrayList();
// 字符串转换成日期
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = format.parse(dates);
Calendar startTime = Calendar.getInstance();
startTime.clear();
startTime.setTime(startDate);
int startYear = startTime.get(Calendar.YEAR);
int startMonth = startTime.get(Calendar.MONTH);
int startDay = startTime.get(Calendar.DAY_OF_MONTH);
Date endDate = format.parse(datee);
Calendar endTime = Calendar.getInstance();
endTime.clear();
endTime.setTime(endDate);
int endYear = endTime.get(Calendar.YEAR);
int endMonth = endTime.get(Calendar.MONTH);
int endDay = endTime.get(Calendar.DAY_OF_MONTH);
int count = 0;
for (int x = startYear; x <= endYear; x++) {
// 罗马历法产生年份公元1582年
int gregorianCutoverYear = 1582;
// 判断是否是闰年
boolean isLeapYear = x >= gregorianCutoverYear ? ((x % 4 == 0) && ((x % 100 != 0) || (x % 400 == 0)))
: (x % 4 == 0);
// 获取开始月的最大天数;大月是1,3,5,7,8,10,12;小月是4,6,9,11;特殊月是2
int max = 0;
if (startMonth == 1) {
if (isLeapYear) {
max = 29;
}
if (!isLeapYear) {
max = 28;
}
}
if (startMonth == 3 || startMonth == 5 || startMonth == 8
|| startMonth == 10) {
max = 30;
}
if (startMonth == 0 || startMonth == 2 || startMonth == 4
|| startMonth == 6 || startMonth == 7 || startMonth == 9
|| startMonth == 11) {
max = 31;
}
// 循环每个月
// 如果在日期范围内月份循环时自增到了一年的最后一个月就将月份初始化到一月份
int y = 0;
// 如果是开始日期的第一个年的月数就从开始月数循环
if (x == startYear) {
y = startMonth;
}
for (; y < 12; y++) {
// 获取当月的最大天数;大月是1,3,5,7,8,10,12;小月是4,6,9,11;特殊月是2
max = 0;
if (y == 1) {
if (isLeapYear) {
max = 29;
}
if (!isLeapYear) {
max = 28;
}
}
if (y == 3 || y == 5 || y == 8 || y == 10) {
max = 30;
}
if (y == 0 || y == 2 || y == 4 || y == 6 || y == 7 || y == 9
|| y == 11) {
max = 31;
}
int ty = y + 1;
// 循环每一天
int z = 1;
// 如果是开始日期的第一个月的天数就从开始天数循环
if (x == startYear && y == startMonth) {
z = startDay;
}
for (; z <= max; z++) {
count++;
dayls.add(x + "-" + ty + "-" + z);
if (x == endYear && y == endMonth && z == endDay) {
break;
}
}
// 如果已经遍历过了截至日期的最后月份就中止月份的循环
if (x == endYear && y == endMonth) {
break;
}
}
}
return dayls;
}
//计算月数
public static List month(String dates, String datee) throws ParseException {
List dayls = new ArrayList();
// 字符串转换成日期
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = format.parse(dates);
Calendar startTime = Calendar.getInstance();
startTime.clear();
startTime.setTime(startDate);
int startYear = startTime.get(Calendar.YEAR);
int startMonth = startTime.get(Calendar.MONTH);
int startDay = startTime.get(Calendar.DAY_OF_MONTH);
Date endDate = format.parse(datee);
Calendar endTime = Calendar.getInstance();
endTime.clear();
endTime.setTime(endDate);
int endYear = endTime.get(Calendar.YEAR);
int endMonth = endTime.get(Calendar.MONTH);
int endDay = endTime.get(Calendar.DAY_OF_MONTH);
// if(startDay!=1){
// startTime.add(Calendar.DAY_OF_MONTH, -startDay);
// }
int count = 0;
for (int x = startYear; x <= endYear; x++) {
// 罗马历法产生年份公元1582年
int gregorianCutoverYear = 1582;
// 判断是否是闰年
boolean isLeapYear = x >= gregorianCutoverYear ? ((x % 4 == 0) && ((x % 100 != 0) || (x % 400 == 0)))
: (x % 4 == 0);
// 获取开始月的最大天数;大月是1,3,5,7,8,10,12;小月是4,6,9,11;特殊月是2
int max = 0;
if (startMonth == 1) {
if (isLeapYear) {
max = 29;
}
if (!isLeapYear) {
max = 28;
}
}
if (startMonth == 3 || startMonth == 5 || startMonth == 8
|| startMonth == 10) {
max = 30;
}
if (startMonth == 0 || startMonth == 2 || startMonth == 4
|| startMonth == 6 || startMonth == 7 || startMonth == 9
|| startMonth == 11) {
max = 31;
}
// 循环每个月
// 如果在日期范围内月份循环时自增到了一年的最后一个月就将月份初始化到一月份
int y = 0;
// 如果是开始日期的第一个年的月数就从开始月数循环
if (x == startYear) {
y = startMonth;
}
for (; y < 12; y++) {
// 获取当月的最大天数;大月是1,3,5,7,8,10,12;小月是4,6,9,11;特殊月是2
max = 0;
if (y == 1) {
if (isLeapYear) {
max = 29;
}
if (!isLeapYear) {
max = 28;
}
}
if (y == 3 || y == 5 || y == 8 || y == 10) {
max = 30;
}
if (y == 0 || y == 2 || y == 4 || y == 6 || y == 7 || y == 9
|| y == 11) {
max = 31;
}
int ty = y + 1;
// 循环每一天
int z = 1;
for (; z <= max; z++) {
count++;
if (z == max || z == 1) {
dayls.add(x + "-" + ty + "-" + z);
}
if (x == endYear && y == endMonth + 1) {
break;
}
}
// 如果已经遍历过了截至日期的最后月份就中止月份的循环
if (x == endYear && y == endMonth) {
break;
}
}
}
return dayls;
}
//计算周数
public List week(String dates, String datee) throws Exception {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = format.parse(dates);
Date endDate = format.parse(datee);
ArrayList ls = new ArrayList();
Calendar beginCalendar = Calendar.getInstance();
beginCalendar.setTime(startDate);
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(endDate);
int sday = beginCalendar.get(Calendar.DAY_OF_WEEK) - 1;
int eday = endCalendar.get(Calendar.DAY_OF_WEEK) - 1;
if (sday != 0) {
beginCalendar.add(Calendar.DAY_OF_WEEK, -sday);
}
if (eday != 0) {
endCalendar.add(Calendar.DAY_OF_WEEK, eday + 7);
} else {
endCalendar.add(Calendar.DAY_OF_WEEK, eday + 1);
}
while (beginCalendar.before(endCalendar)) {
if (beginCalendar.get(Calendar.DAY_OF_WEEK) - 1 == 0) {
ls.add(format.format(beginCalendar.getTime()));
}
beginCalendar.add(Calendar.DAY_OF_YEAR, 1);
}
return ls;
}
JAVA报表两日期间月,周,日计算
最新推荐文章于 2021-02-27 11:05:08 发布