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;

}

java表格计算,JAVA表格两日期间月,周,日计算相关推荐

  1. php 计算日期差几周,PHP计算两个时间之差的函数(年,月,周,日,小时,分钟,秒数)

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 PHP 2  // 时间格式为YYYYMMDDHHmmss 3  function timeDiff( $aTime , $bTime ) 4 { 5   ...

  2. 数学与计算机科学国际研讨会怎么样,“计算数学与科学计算国际研讨会——暨纪念冯康先生百年华诞”(2020年8月17日-8月20日)...

    原标题:"计算数学与科学计算国际研讨会--暨纪念冯康先生百年华诞"(2020年8月17日-8月20日) 诚挚邀请您参加于2020年8月17日--8月20日由中国科学院数学与系统科学 ...

  3. 分享Silverlight/WPF/Windows Phone/HTML5一周学习导读(1月9日-1月15日)

    分享Silverlight/WPF/Windows Phone/HTML5一周学习导读(1月9日-1月15日) 本周Silverlight学习资源更新 Silverlight4Beta之Binding ...

  4. Windows8/Silverlight/WPF/WP7周学习导读(11月12日-11月18日)

    Windows8/Silverlight/WPF/WP7/HTML5周学习导读(11月12日-11月18日) 本周Windows 8开发学习资源更新 快速构建Windows 8风格应用25-数据绑定 ...

  5. 关于2020年8月7日—8月29日实习学习到的内容

    关于2020年8月7日-8月29日实习学习到的内容 我是一名大三(准大四)的学生,本专业学习通信工程,从三月份自学前端至今,七月份找到了一份实习,感觉在实习中学习到的内容会比较深入一点叭.关于这篇文章 ...

  6. Silverlight/Windows8/WPF/WP7/HTML5周学习导读(10月15日-10月21日)

    Silverlight/Windows8/WPF/WP7/HTML5周学习导读(10月15日-10月21日) 本周Silverlight学习资源更新 Silverlight + DomainServi ...

  7. 青岛Uber优步司机奖励政策(9月14日~9月20日)

    由于上周银行系统升级,工资延后 9/14-9/20奖励细则 滴滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不 ...

  8. 分享Silverlight/Windows8/WPF/WP7/HTML5一周学习导读(5月21日-5月26日)

    分享Silverlight/Windows8/WPF/WP7/HTML5一周学习导读(5月21日-5月26日) 本周Silverlight学习资源更新 Silverlight Super TextBo ...

  9. Silverlight/Windows8/WPF/WP7/HTML5周学习导读(7月16日-7月22日)

    Silverlight/Windows8/WPF/WP7/HTML5周学习导读(7月16日-7月22日) 本周Silverlight学习资源更新 Silverlight DataGrid用法 模板列 ...

最新文章

  1. 电子计算机微机调试员,电子计算机(微机)装配调试员国家职业标准
  2. 从抵触到力推,.Net Core 的成功让微软正视开源
  3. 使用BCH彩色币方案发行Token已实现
  4. linux 备份svn
  5. myelicpes怎么导入PHP项目,利用PHP执行SQL文件,将SQL文件导入到数据库
  6. 资源放送丨《高并发Oracle OLTP系统的故障案例分享》PPT视频
  7. 【idea】 Unsupported class file major version 57
  8. 【九天教您南方cass 9.1】02 从地形图上绘制纵横断面
  9. 日语毕业论文日文参考文献怎么找?
  10. STM32Cube软件安装图文教程及视频演示
  11. LeetCode每日一题(22年1月27日-2月5日)
  12. WiFi的信道与关联
  13. Skyscrapers (hard version)(1900/单调栈)
  14. TF卡里删掉文件后内存没变大_双11,TF卡,SD卡,读卡器如何选,看这篇就够了...
  15. mybatis 整合spring之mapperLocations配置的问题
  16. GNSS和GPS的区别
  17. 如何使用sizeof获取指针指向数组的长度
  18. 冗余设计之时间冗余、资源冗余
  19. 自学脚手架——《热学》 by 李椿(第一,二,三,四,五章)
  20. OpenLayers加载WMS

热门文章

  1. Could not connect to wpa_supplicant: p2p-dev-wlan0 - re-trying
  2. 新手程序员去哪里?避雷小技巧交给你
  3. 包装设计实战案例教学
  4. i510300h和r54600h的区别 哪个好
  5. 在数据集Euroc v dataset下跑双目ORB-SLAM2
  6. 我为Bill Gates熬夜加班的那个晚上
  7. 用amd组装高档游戏型计算机,高配吃鸡真带劲!高端最新只选AMD游戏主机
  8. 计算机保持在线的几种方法,还在为智能盒子上电影软件收费烦恼?教你几个盒子上看大片的方法...
  9. git:SSL证书问题:无法获取本地颁发者证书
  10. KT148A语音芯片ic工作原理以及芯片的内部架构描述