java 年月_Java 年月的计算

/**

* 获取指定年月的第一天

* @param year

* @param month

* @return

*/

public static Date getFirstDayOfMonth1(int year, int month) throws ParseException {

Calendar cal = Calendar.getInstance();

//设置年份

cal.set(Calendar.YEAR, year);

//设置月份

cal.set(Calendar.MONTH, month-1);

//获取某月最小天数

int firstDay = cal.getMinimum(Calendar.DATE);

//设置日历中月份的最小天数

cal.set(Calendar.DAY_OF_MONTH,firstDay);

//格式化日期

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

Date date =sdf.parse(sdf.format(cal.getTime()));

return date;

}

/**

* 获取指定年月的最后一天

* @param year

* @param month

* @return

*/

public static Date getLastDayOfMonth1(int year, int month) throws ParseException {

Calendar cal = Calendar.getInstance();

//设置年份

cal.set(Calendar.YEAR, year);

//设置月份

cal.set(Calendar.MONTH, month-1);

//获取某月最大天数

int lastDay = cal.getActualMaximum(Calendar.DATE);

//设置日历中月份的最大天数

cal.set(Calendar.DAY_OF_MONTH, lastDay);

//格式化日期

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

Date date =sdf.parse(sdf.format(cal.getTime()));

return date;

}

/*时间比大小*/

public int timeCompare(String t1,String t2){

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");

Calendar c1=Calendar.getInstance();

Calendar c2=Calendar.getInstance();

try {

c1.setTime(formatter.parse(t1));

c2.setTime(formatter.parse(t2));

} catch (ParseException e) {

e.printStackTrace();

}

int result=c1.compareTo(c2);

return result;

}

/*时间相减获取天数*/

public static int differentDaysByMillisecond(Date date1,Date date2)

{

// int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));

long days=(date2.getTime()-date1.getTime())/(24*60*60*1000)+1;

return (int) days;

}

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

String[] time= statistics.getAttenceDate().split("-");if (time.length!=2){

ResultUtil.error("年月格式不规范");

}

askLeaves.forEach(askLeave->{try{//开始日期结束日期都在当前月

if(timeCompare(statistics.getAttenceDate(),askLeave.getStartDate())==0&&timeCompare(statistics.getAttenceDate(),askLeave.getEndDate())==0){//获取请假天数

int day =differentDaysByMillisecond(sdf.parse(askLeave.getStartDate()),sdf.parse(askLeave.getEndDate()));if(hashMap.get(askLeave.getLeaveTypeCode())==null){

hashMap.put(askLeave.getLeaveTypeCode(),day);

}else{

hashMap.put(askLeave.getLeaveTypeCode(),hashMap.get(askLeave.getLeaveTypeCode())+day);

}

}//开始月在当前月结束月不在

if(timeCompare(statistics.getAttenceDate(),askLeave.getStartDate())==0&&timeCompare(statistics.getAttenceDate(),askLeave.getEndDate())!=0) {// //获取指定月最后一天

int day = differentDaysByMillisecond(sdf.parse(askLeave.getStartDate()),getLastDayOfMonth1(Integer.valueOf(time[0]),Integer.valueOf(time[1])));

hashMap.put(askLeave.getLeaveTypeCode(),day);

}//结束月在当前月开始月不在

if(timeCompare(statistics.getAttenceDate(),askLeave.getStartDate())!=0&&timeCompare(statistics.getAttenceDate(),askLeave.getEndDate())==0) {//calendar.add(Calendar.MONTH, 0);//获取指定月第一天

int day = differentDaysByMillisecond(getFirstDayOfMonth1(Integer.valueOf(time[0]),Integer.valueOf(time[1])),sdf.parse(askLeave.getEndDate()));

hashMap.put(askLeave.getLeaveTypeCode(),day);

}

}catch(Exception e) {

e.printStackTrace();

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值