Java根据开始时间 结束时间 金额 跨月份 计算总价

package com.zgyanglao.saas.ams.util;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * @author tianyuqing
 * @version V1.0
 * @Package com.zgyanglao.saas.ams.util
 * @date 2020/5/7 11:10
 */
public class CheckPrice {

	public static int isSameDate(String date1, String date2) throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(sdf.parse(date1));

		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(sdf.parse(date2));

		boolean isSameYear = cal1.get(Calendar.YEAR) == cal2
			.get(Calendar.YEAR);
		int isSameMonth = cal2.get(Calendar.MONTH) - cal1.get(Calendar.MONTH);
		if(isSameYear&&isSameMonth==0){
			return 1;//同年同月
		}
		if(isSameYear&&isSameMonth==1){
			return 2;//同年且月份相邻
		}
		return 3;
	}

	private static String getDifference(String start, String end) throws Exception{
		String backStr = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String str1 = start;
		String str2 = end;
		Calendar bef = Calendar.getInstance();
		Calendar aft = Calendar.getInstance();

		bef.setTime(sdf.parse(str1));
		aft.setTime(sdf.parse(str2));

		int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);//结束月份减开始月份
		int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;//结束年份减开始年份*12
		if(isSameDate(start,end)==1){//同年同月
			int day = aft.get(Calendar.DAY_OF_MONTH) - bef.get(Calendar.DAY_OF_MONTH);//结束时间减开始时间
			backStr = day+1+"@_@"+bef.getActualMaximum(Calendar.DAY_OF_MONTH);//入住时间 本月最大天数
		}
		if(isSameDate(start,end)==2){//同年且月份相邻
			backStr = (bef.getActualMaximum(Calendar.DAY_OF_MONTH)-bef.get(Calendar.DAY_OF_MONTH)+1)//开始时间最大天数-开始时间=开始月住了多少天
				+"@_@"+aft.get(Calendar.DAY_OF_MONTH)+"@_@"+bef.getActualMaximum(Calendar.DAY_OF_MONTH)+"@_@"+aft.getActualMaximum(Calendar.DAY_OF_MONTH);//结束月住了多少天 开始月最大天数 结束月最大天数
		}
		if(isSameDate(start, end)==3){
			backStr = (Math.abs(month + result)-1)+"@_@"+(bef.getActualMaximum(Calendar.DAY_OF_MONTH)-bef.get(Calendar.DAY_OF_MONTH)+1)//足月月份 开始月住了多少天
				+"@_@"+aft.get(Calendar.DAY_OF_MONTH)+"@_@"+bef.getActualMaximum(Calendar.DAY_OF_MONTH)+"@_@"+aft.getActualMaximum(Calendar.DAY_OF_MONTH);//结束月住了多少天 开始月最大天数 结束月最大天数
		}
		return backStr;
	}


	public static BigDecimal getMoney(String start, String end, double money) throws Exception{
		String backStr = getDifference(start,end);
		String[] backStrs = backStr.split("@_@");
		double price = 0;
		//两个日期是同年同月时
		if(isSameDate(start,end)==1){
			int days = Integer.parseInt(backStrs[0]);
			int maxDays = Integer.parseInt(backStrs[1]);
			price = money/maxDays*days;
		}
		//两个日期同年且月份相邻
		if(isSameDate(start,end)==2){
			int days1 = Integer.parseInt(backStrs[0]);
			int maxDays1 = Integer.parseInt(backStrs[2]);
			int days2 = Integer.parseInt(backStrs[1]);
			int maxDays2 = Integer.parseInt(backStrs[3]);
			price =  money/maxDays1*days1+money/maxDays2*days2;
		}
		if(isSameDate(start,end)==3){
			int month = Integer.parseInt(backStrs[0]);
			int days1 = Integer.parseInt(backStrs[1]);
			int maxDays1 = Integer.parseInt(backStrs[3]);
			int days2 = Integer.parseInt(backStrs[2]);
			int maxDays2 = Integer.parseInt(backStrs[4]);
			price =  money*month + money/maxDays1*days1 + money/maxDays2*days2;
		}
		BigDecimal b = new BigDecimal(price);
		return b.setScale(2,BigDecimal.ROUND_HALF_UP);
	}
}

你可能感兴趣的:(Java根据开始时间 结束时间 金额 跨月份 计算总价)