日期计算
作者:互联网
/**
* 自然月拆分
* @param beginDate 开始时间
* @return
* beginDate 开始时间
* endDate 本月结束时间
* preDate 下月开始时间
* dateNum 开始及结束时间之间的天数
* mothNum 当月天数
*/
public static ExtendMap<String, Object> naturalMonthSplit(Date beginDate){
Date preDate = DateUtils.ceiling(beginDate, Calendar.MARCH) ; // 2018-04-01
Date endDate = DateUtils.addDays(preDate, -1) ; // 2018-03-31
long dateNum = DataTimeUtils.differentDate("DAY", beginDate, endDate)+ 1 ; //26
long mothNum = DataTimeUtils.differentDate("DAY", getPreMonthDate(beginDate), endDate)+1 ; //31
ExtendMap<String, Object> returnMap = new ExtendMap<>();
returnMap.put("beginDate",beginDate);
returnMap.put("endDate",endDate);
returnMap.put("preDate", preDate);
returnMap.put("dateNum", dateNum);
returnMap.put("mothNum", mothNum);
return returnMap ;
}
/**
* 自然月拆分
* 获取开始时间到结束时间中每个月的时间及天数
* @param beginDate 开始时间
* @param endDate 结束时间
* @return
*/
public static List<ExtendMap<String, Object>> naturalMonthSplit(Date beginDate , Date endDate){
List<ExtendMap<String, Object>> returnList = new ArrayList<>();
long num = DataTimeUtils.differentDate("M", beginDate, endDate);
for(long i=0; i<=num ; i++) {
ExtendMap<String, Object> map = new ExtendMap<>();
map = naturalMonthSplit(beginDate);
if(i == num){
map.put("endDate", endDate);
map.put("dateNum", DataTimeUtils.differentDate("D", (Date) map.get("beginDate"), endDate)+1);
}
returnList.add(map);
beginDate = (Date) map.get("preDate");
}
return returnList ;
}
/**
* 合同月拆分
* @param beginDate
* @return
*/
public static ExtendMap<String, Object> contractMonthSplit(Date beginDate) {
ExtendMap<String, Object> map = new ExtendMap<>();
Calendar cal1 = Calendar.getInstance();
cal1.setTime(beginDate);
int day = cal1.get(Calendar.DAY_OF_MONTH);
Date afterDate;
if(day > 27 ) {
Date endDate = DateUtils.addMonths(beginDate, 1);
Date beginDateTemp =DateUtils.addDays(getPreMonthDate(endDate),-1) ;
int dayEndNum = (int) DataTimeUtils.differentDate("D", beginDate, beginDateTemp) + 1 ;
afterDate= DateUtils.addDays(getEndMonthDate(endDate), -dayEndNum);
map.put("beginDate", beginDate);
map.put("endDate", afterDate);
}else {
afterDate = DateUtils.addDays( DateUtils.addMonths(beginDate, 1),-1);
map.put("beginDate", beginDate);
map.put("endDate", afterDate);
}
long dateNum = DataTimeUtils.differentDate("DAY", beginDate, afterDate)+ 1 ; //26
long mothNum = DataTimeUtils.differentDate("DAY", beginDate, afterDate)+ 1 ;
map.put("dateNum", dateNum);
map.put("mothNum", mothNum);
return map ;
}
/**
* 合同月拆分
* @param beginDate
* @param endDate
* @return
*/
public static List<ExtendMap<String, Object>> contractMonthSplit(Date beginDate , Date endDate) {
List<ExtendMap<String, Object>> returnList = new ArrayList<>();
long num = DataTimeUtils.differentDate("M", beginDate, endDate);
Date tempDate = beginDate ;
for(int i=0;i<num;i++) {
ExtendMap<String, Object> map =contractMonthSplit(tempDate);
tempDate = (Date) map.get("endDate");
tempDate = DateUtils.addDays(tempDate, 1);
if(i == num){
map.put("endDate", endDate);
map.put("dateNum", DataTimeUtils.differentDate("D", (Date) map.get("beginDate"), endDate)+1);
}
returnList.add(map);
}
return returnList ;
}
/**
* 日期拆分调用
* @param beginDate
* @param endDate
* @param type
* @return
*/
public static List<ExtendMap<String, Object>> dateSplit(Date beginDate , Date endDate , Long type ) {
if(type == 1) {
return naturalMonthSplit(beginDate, endDate);
}else{
return contractMonthSplit(beginDate, endDate);
}
}
/**
* 计算开始日期到结束日期之间月数
* @param begDate 开始日期
* @param endDate 结束日期
* @return
*/
public static BigDecimal calculationAmount(Date begDate,Date endDate) {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(begDate);
cal1.add(Calendar.MONTH, 1);
cal1.set(Calendar.DAY_OF_MONTH, cal1.getActualMinimum(Calendar.DAY_OF_MONTH));
Date preMonth =cal1.getTime(); //开始日期的下一个月
Calendar cal2 = Calendar.getInstance();
cal2.setTime(endDate);
cal2.add(Calendar.MONTH, -1);
cal2.set(Calendar.DAY_OF_MONTH, cal2.getActualMaximum(Calendar.DAY_OF_MONTH));
Date lastMonth = cal2.getTime(); //结束日期的上一个月
long midValue = 0;//开始日期的下一个月和结束日期的上一个月之间有多少月
if(preMonth.before(lastMonth)) {
midValue = DataTimeUtils.differentDate("M", preMonth, lastMonth)+1;
}
long begDateNum = DataTimeUtils.differentDate("D", begDate, getEndMonthDate(begDate))+1;//开始日期到当前月底的的天数
long begMouthNum = getCurrentMonthDay(begDate);//开始日期当前月有多少天
BigDecimal begValue = new BigDecimal(begDateNum).divide(new BigDecimal(begMouthNum),6,BigDecimal.ROUND_HALF_UP);
long endDateNum = DataTimeUtils.differentDate("D", getPreMonthDate(endDate),endDate)+1;//结束日期当月的第一天到结束日期的天数
long endMouthNum = getCurrentMonthDay(endDate);//结束日期当前月有多少天
BigDecimal endValue = new BigDecimal(endDateNum).divide(new BigDecimal(endMouthNum),6,BigDecimal.ROUND_HALF_UP);
BigDecimal sumDate =begValue.add(endValue.add(new BigDecimal(midValue)));
return sumDate;
}
/**
* 判断日期是否在自然季内
*/
public static int getNaturalSeason(Date begDate) {
Calendar beg = Calendar.getInstance();//设置开始日期
beg.setTime(begDate);
if((beg.get(Calendar.MONTH)+1)>0 && (beg.get(Calendar.MONTH)+1)<=3) {
return 1;
}else if((beg.get(Calendar.MONTH)+1)>=4 && (beg.get(Calendar.MONTH)+1)<=6) {
return 2;
}else if((beg.get(Calendar.MONTH)+1)>=7 && (beg.get(Calendar.MONTH)+1)<=9) {
return 3;
}else if((beg.get(Calendar.MONTH)+1)>=10 && (beg.get(Calendar.MONTH)+1)<=12) {
return 4;
}
return 0;
}
/**
* 判断日期是否在自然半年内
*/
public static int getNaturalHalfYear(Date begDate) {
Calendar beg = Calendar.getInstance();//设置开始日期
beg.setTime(begDate);
if((beg.get(Calendar.MONTH)+1)>0 && (beg.get(Calendar.MONTH)+1)<=6) {
return 1;
}else if((beg.get(Calendar.MONTH)+1)>=7 && (beg.get(Calendar.MONTH)+1)<=12) {
return 2;
}
return 0;
}
/**
* 判断日期是否在自然年内
*/
public static int getNaturalYear(Date begDate) {
Calendar beg = Calendar.getInstance();//设置开始日期
beg.setTime(begDate);
if((beg.get(Calendar.MONTH)+1)>0 && (beg.get(Calendar.MONTH)+1)<=12) {
return 1;
}
return 0;
}
/**
* 根据开始日期和结束日期判断在哪个时间段中
* @param nowStartTime 月租金的开始日期
* @param nowEndTime 月租金的结束日期
* @param StartTime 合同租金的开始日期
* @param endTime 合同租金的结束日期
* @return
*/
public static int isEffectiveDate(Date nowStartTime,Date nowEndTime, Date StartTime, Date endTime) {
Calendar date1 = Calendar.getInstance();//月租金的开始日期
date1.setTime(nowStartTime);
Calendar date2 = Calendar.getInstance();// 月租金的结束日期
date2.setTime(nowEndTime);
Calendar begin = Calendar.getInstance();//合同租金的开始日期
begin.setTime(StartTime);
Calendar end = Calendar.getInstance();//合同租金的结束日期
end.setTime(endTime);
//月租金全部包含在合同租金
if((date1.after(begin) || date1.equals(begin)) && (date2.before(end) || date2.equals(end))) {
return 1;
//免租金全部包含在月租金中
}else if((date1.before(begin) || date1.equals(begin)) && (date2.after(end) || date2.equals(end))) {
return 2;
//月租金的开始日期包含在合同租金中
}else if((date1.after(begin) || date1.equals(begin)) && date2.after(end) && date1.before(end)){
return 3;
//月租金的结束日期包含在合同租金中
}else if(date1.before(begin) && date2.before(end) && (date2.after(begin) || date2.equals(begin))){
return 4;
}else {
return 0;
}
}
/**
* 获取任意时间的月第一天
* @param repeatDate
* @return
*/
public static Date getPreMonthDate(Date repeatDate){
return DateUtils.truncate(repeatDate,Calendar.MARCH);
}
/**
* 获取任意时间的月的最后一天
* @param repeatDate
* @return
*/
public static Date getEndMonthDate(Date repeatDate) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(repeatDate);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return calendar.getTime();
}
/**
* 根据日期计算当月有多少天
* @param date
* @return
*/
public static long getCurrentMonthDay(Date date) {
Calendar a = Calendar.getInstance();
a.setTime(date);
a.set(Calendar.DATE, 1);
a.roll(Calendar.DATE, -1);
int maxDate = a.get(Calendar.DATE);
return maxDate;
}
/**
* 判断时间格式
*/
public static boolean isValidDate(String str) {
boolean convertSuccess=true;
// 指定日期格式为四位年/两位月份/两位日期,注意yyyy-MM-dd区分大小写;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007-02-29会被接受,并转换成2007-03-01
format.setLenient(false);
format.parse(str);
} catch (ParseException e) {
// 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
convertSuccess=false;
}
return convertSuccess;
}
/**
* 数据的奇偶校验
*
*/
public static int Number(String str) {
//先将字符转成2进制
String str1 = "";
char[] strChar = str1.toCharArray();
String result = "";
for (int i = 0; i < strChar.length; i++) {
result += Integer.toBinaryString(strChar[i]) + " ";
}
//循环找出二进制数字中1的个数
int a1 = 0;
for (int i = 0; i < result.length(); i++) {
String c = result.substring(i, i + 1);
if (c.equalsIgnoreCase("1")) {
a1++;
}
}
int b = a1 % 2 == 0 ? 0 : 1;
return b;
}
/**
*
* @param ts MinimumGuaranteeDomain 的list
* @return false 时间交叉 true 时间不交叉
*/
public static boolean check(List<MinimumGuaranteeDomain> ts ) {
Collections.sort(ts, new MinimumGuaranteeDomain());
boolean flag=true;
for(int i=0; i<ts.size()-1;i++) {
MinimumGuaranteeDomain item1=ts.get(i);;
MinimumGuaranteeDomain item2=ts.get(i+1);
Date end = new Date(DataTimeUtils.getTimeLong(item1.getEnddate())*1000);
Date beg = new Date(DataTimeUtils.getTimeLong(item2.getBegdate())*1000);
if(DataTimeUtils.differentDate("D",end,beg)<0) {
flag=false;
break;
}
}
return flag ;
}
标签:map,endDate,get,beginDate,日期,计算,Date,Calendar 来源: https://www.cnblogs.com/gemiaomiao/p/10550746.html