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<Integer, BigDecimal> getPerPeriodInterest(double invest, double periodRate, int periodMonthNum) {
Map<Integer, BigDecimal> map = new HashMap<Integer, BigDecimal>();
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<Integer, BigDecimal> 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<Integer, BigDecimal> mapInterest = getPerPeriodInterest(invest, periodRate, periodNum);
Map<Integer, BigDecimal> mapPrincipal = new HashMap<Integer, BigDecimal>();
for (Map.Entry<Integer, BigDecimal> 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<Integer, BigDecimal> mapInterest = getPerPeriodInterest(invest, periodRate, periodNum);
for (Map.Entry<Integer, BigDecimal> 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<Integer, BigDecimal> mapInterest = getPerPeriodInterest(invest, periodRate, periodNum);
for (Map.Entry<Integer, BigDecimal> entry : mapInterest.entrySet()) {
perMonthInterest = perMonthInterest.add(entry.getValue());
}
return perMonthInterest.doubleValue();
}
/**
* 每期還款日期
* @param start_date
* 起租日
* @param perPeriodMonthNum
* 每期月數
* @param periodNum
* 期數
* @return
*/
public static Map<Integer, String> getRepaymentDate(String start_date, int perPeriodMonthNum, int periodNum) {
Map<Integer, String> periodRepaymentDate = new HashMap<Integer, String>();
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;
}
/**
* 功能描述:返回指定日期加上多少月之後的時間<BR>
* @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<Integer, BigDecimal> mapInterest = getPerPeriodInterest(invest, periodRate, periodNum);
System.out.println("等額本息---每期還款利息:" + mapInterest);
Map<Integer, BigDecimal> 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實現等額本息
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.