Java实现等额本息

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

/**
 * 等额本息
 */
public class EqualPrincipalInterestUtil {

	/**
	 * 每期本金+利息
	 * 公式:每月偿还本息=〔贷款本金×月利率×(1+月利率)^还款月数〕÷〔(1+月利率)^还款月数-1)
	 * @param invest
	 *            本金
	 * @param periodRate
	 *            每期利率
	 * @param periodMonthNum
	 *            期数
	 * @return
	 */
	public static double getPerPeriodPrincipalInterest(double invest, double periodRate, int periodMonthNum) {
		BigDecimal monthIncome = new BigDecimal(invest)
				.multiply(new BigDecimal(periodRate * Math.pow(1 + periodRate, periodMonthNum)))
				.divide(new BigDecimal(Math.pow(1 + periodRate, periodMonthNum) - 1), 4, BigDecimal.ROUND_HALF_UP);
		return monthIncome.doubleValue();
	}

	/**
	 * 每期利息
	 * 公式:每月偿还利息=贷款本金×月利率×〔(1+月利率)^还款月数-(1+月利率)^(还款月序号-1)〕÷〔(1+月利率)^还款月数-1〕
	 * @param invest
	 *            本金
	 * @param periodRate
	 *            每期利率
	 * @param periodMonthNum
	 *            期数
	 * @return
	 */
	public static Map getPerPeriodInterest(double invest, double periodRate, int periodMonthNum) {
		Map map = new HashMap();
		BigDecimal periodInterest;
		for (int i = 1; i < periodMonthNum + 1; i++) {
			BigDecimal multiply = new BigDecimal(invest).multiply(new BigDecimal(periodRate));
			BigDecimal sub = new BigDecimal(Math.pow(1 + periodRate, periodMonthNum))
					.subtract(new BigDecimal(Math.pow(1 + periodRate, i - 1)));
			periodInterest = multiply.multiply(sub).divide(new BigDecimal(Math.pow(1 + periodRate, periodMonthNum) - 1),
					6, BigDecimal.ROUND_HALF_UP);
			periodInterest = periodInterest.setScale(4, BigDecimal.ROUND_HALF_UP);
			map.put(i, periodInterest);
		}
		return map;
	}

	/**
	 * 每期还款本金
	 * 
	 * @param invest
	 *            本金
	 * @param periodRate
	 *            每期利率
	 * @param periodNum
	 *            期数
	 * @return
	 */
	public static Map getPerPeriodPrincipal(double invest, double periodRate, int periodNum) {
		BigDecimal monthIncome = new BigDecimal(invest)
				.multiply(new BigDecimal(periodRate * Math.pow(1 + periodRate, periodNum)))
				.divide(new BigDecimal(Math.pow(1 + periodRate, periodNum) - 1), 4, BigDecimal.ROUND_HALF_UP);
		Map mapInterest = getPerPeriodInterest(invest, periodRate, periodNum);
		Map mapPrincipal = new HashMap();

		for (Map.Entry entry : mapInterest.entrySet()) {
			mapPrincipal.put(entry.getKey(), monthIncome.subtract(entry.getValue()));
		}
		return mapPrincipal;
	}

	/**
	 * 总利息
	 * 
	 * @param invest
	 *            本金
	 * @param periodRate
	 *            每期利率
	 * @param periodNum
	 *            期数
	 * @return
	 */
	public static double getInterestCount(double invest, double periodRate, int periodNum) {
		BigDecimal count = new BigDecimal(0);
		Map mapInterest = getPerPeriodInterest(invest, periodRate, periodNum);

		for (Map.Entry entry : mapInterest.entrySet()) {
			count = count.add(entry.getValue());
		}
		return count.doubleValue();
	}

	/**
	 * 本息总和
	 * 
	 * @param invest
	 *            本金
	 * @param periodRate
	 *            每期利率
	 * @param periodNum
	 *            期数
	 * @return
	 */
	public static double getPrincipalInterestCount(double invest, double periodRate, int periodNum) {
		BigDecimal perMonthInterest = new BigDecimal(invest);
		Map mapInterest = getPerPeriodInterest(invest, periodRate, periodNum);
		for (Map.Entry entry : mapInterest.entrySet()) {
			perMonthInterest = perMonthInterest.add(entry.getValue());
		}
		return perMonthInterest.doubleValue();
	}

	/**
	 * 每期还款日期
	 * @param start_date
	 * 				起租日
	 * @param perPeriodMonthNum
	 * 				每期月数
	 * @param periodNum
	 * 				期数
	 * @return
	 */
	public static Map getRepaymentDate(String start_date, int perPeriodMonthNum, int periodNum) {
		Map periodRepaymentDate = new HashMap();
		String nextRepaymentDate = start_date;
		periodRepaymentDate.put(1, nextRepaymentDate);
		for (int i = 2; i < periodNum + 1; i++) {
			nextRepaymentDate = getMonthAdd(perPeriodMonthNum, nextRepaymentDate, "yyyyMMdd");
			periodRepaymentDate.put(i, nextRepaymentDate);
		}
		return periodRepaymentDate;
	}

	/**
	 * 功能描述:返回指定日期加上多少月之后的时间
* @param from yyyyMMdd * @param day * @param formatStr * @return */ public static String getMonthAdd(int day,String from,String formatStr){ SimpleDateFormat sdf=new SimpleDateFormat(formatStr); Calendar calendar = Calendar.getInstance(); try { calendar.setTime(sdf.parse(from)); } catch (Exception e) { e.printStackTrace(); } calendar.add(Calendar.MONTH, day); String date = sdf.format(calendar.getTime()); return date; } public static void main(String[] args) { double invest = 10000; // 本金 int periodNum = 4; double periodRate = 0.12/3; // 年利率 System.out.println("等额本息---本金:" + invest); double perMonthPrincipalInterest = getPerPeriodPrincipalInterest(invest, periodRate, periodNum); System.out.println("等额本息---每期还款本息:" + perMonthPrincipalInterest); Map mapInterest = getPerPeriodInterest(invest, periodRate, periodNum); System.out.println("等额本息---每期还款利息:" + mapInterest); Map mapPrincipal = getPerPeriodPrincipal(invest, periodRate, periodNum); System.out.println("等额本息---每期还款本金:" + mapPrincipal); double count = getInterestCount(invest, periodRate, periodNum); System.out.println("等额本息---总利息:" + count); double principalInterestCount = getPrincipalInterestCount(invest, periodRate, periodNum); System.out.println("等额本息---应还本息总和:" + principalInterestCount); } }

你可能感兴趣的:(java工具)