JAVA报表两日期间月,周,日计算

 

 //计算天数
 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;
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值