//计算天数
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、C#、Ruby等语言也有类似的日期处理方法,通过日期对象的属性或方法计算两个日期间的差异。 5. **工具和库**: 在数据分析领域,像Pandas(Python)和R语言的`difftime`函数也提供了简便的日期差计算...
### 清算/报表/日终跑批程序之性能优化案例(一) #### 环境背景 在本文中,我们将探讨一个与清算/报表/日终跑批程序相关的性能优化案例。本案例由一位拥有丰富数据库性能优化经验的专业人士(以下简称“小y”)...
邱枫同学在2012年1月1日至2012年1月21日之间,在郑州软帝信息科技有限公司的实习经历,正好体现了这一目的。郑州软帝信息科技有限公司是一家在软件开发和服务领域具有特色的企业,不仅服务于金融、电信等行业,还为...
(10)选择“每月结算”,在弹出的窗体中进行对年月消费进行结算,计算出兄弟成员每人的消费收支。 (11)选择“每年结算”,在弹出的窗体中进行对兄弟盟成员消费年结算。 (12)选择“月统计报表”,在弹出的窗体中...
- 诸如计算下一个工作日、计算两个日期间的周末数量等复杂任务,通常需要自定义逻辑或使用特定库(如Java的`Joda-Time`或Python的`BusinessDay`库)。 了解这些基本的日期处理类和方法后,开发者能够轻松处理各种...
在编程中,特别是使用Java语言时,这样的需求非常普遍,尤其是在数据库操作、日志记录、报表生成等场景中经常需要计算两个日期间的天数或者日期列表。 描述中提到了一种使用if else逻辑判断的方法,并指出这种方法...
- **Python中的datetime模块**:提供了datetime、date和time类,以及timedelta类用于日期间的运算。 - **JavaScript中的Date对象**:虽然基本功能有限,但通过引入Moment.js或Luxon等第三方库,可以极大增强其处理...
- 生成租借公司的日、月、季度、年度财务报表。 ##### 题目四:医院管理系统 - **背景**:实现药品、诊疗、医师、病人、病房等信息的管理。 - **要求**: - 提供面向公众的导医和收费标准明细查询功能。 - 挂号...
b) 此软件可能包含某些插件组件,简称插件,其中包括 ActiveX Control、Java 插件和 RA XTRA 插件。 您仅可调用或通过使用 realplay、rcansplg.so.6.0、rpnp.so、rpnphelper、rcaembed.so.6.0、rcacore.so.6.0、...