Calculation of interest in Java ---> 1. Equal principal and interest 2. First interest and then principal

Two kinds of calculations about interest have been sorted out (it can be written as a special tool class, of course, I am an entry-level player, I wrote it according to my own ideas, only for the reference of entry-level friends like me):

public class MathAboutBorrow {
public static Double borrowMoney;
public static Integer month;
public static Double prate;
private static Double [] rate1 = new Double [month] ;//Define an array to store the money that needs to be paid every month in the cycle
private static Double [] rate2 = new Double [month] ;
private static double amountRate1 = 0.0;//The total amount to be repaid each month
private static double amountRate2 = 0.0;//The total amount to be repaid each month

// Determine the repayment date
public static Integer getRepayDay () {
SimpleDateFormat sdf = new SimpleDateFormat("dd");//The date you need to get is today's specific date in the current month
String _repayDay = sdf.format(new Date());
int _day = Integer.parseInt(_repayDay);
Integer repayDay = null;

//According to today's date, determine which day the repayment date is (divided into three stages: the first and middle stages, and the 6th in the middle is the repayment date, which can be defined according to your own product)
if(( _day <= 5 && _day > 0 ) || (_day > 25 && _day <= 31)){
repayDay = 6;
}else if(_day > 5 && _day <= 15){
repayDay = 16;
}else if(_day > 15 && _day <= 25){
repayDay = 26;
}

Integer betweenTime = repayDay - _day;
return betweenTime;
}


//The amount of the monthly repayment after interest first--->recorded by the array
public static Double[] periodFristMonth(){
Integer betweenTime = getRepayDay ();
Calendar calen = Calendar.getInstance();
Date date = new Date();
//The total repayment amount and monthly repayment amount after interest first
for(int i=1;i<=month;i++){


long startDay = date.getTime();//Get the current time, convert it to milliseconds
calen.add(Calendar.MONTH, 1);//Define a time--->Add one month
long endDay = calen.getTime().getTime();//Get the millisecond value of the month defined above
//Get the difference between the defined next month's time and the current time, and the difference between the repayment date we defined, before the repayment date, the default number of days should be added
//(The loan interest takes effect on the day of the loan, and when the default repayment date is not reached in the first month, the interest for the time difference will also be added)
long days = (endDay - startDay)/3600/24/1000+betweenTime;
double periodMonth = prate*days;

rate1[i-1] = borrowMoney*periodMonth;//Write this month's interest into the array
calen.setTime (calen.getTime ());
date = calen.getTime();

}
rate1[month-1] = rate1[month-1] + borrowMoney;//The principal should be repaid in the last month after interest first, so the last month's repayment is the last month's interest + principal

return rate1;//Return the array of interest first and then book
}

//Calculate the total amount of interest first and then principal
public static Double repayPeriodFrist(){
rate1 = periodFristMonth();
for(int i=1;i<=month;i++){
amountRate1 += rate1[i-1];
}
amountRate1 += borrowMoney;//To get the total repayment amount of interest first and then principal, add the loan amount in the last month after interest and principal
return amountRate1;
}

//The amount of equal repayment monthly repayment--->recorded by the array
public static Double[] periodSameMonth(){
Calendar calen = Calendar.getInstance();
Date date = new Date();
long rate2startDay = date.getTime();//Get the current time, convert it to milliseconds
calen.add(Calendar.MONTH, month);
long rate2endDay = calen.getTime().getTime();
long avgdays = (rate2endDay - rate2startDay)/3600/24/1000/month;
double monthPrate = prate*avgdays;
//The total repayment amount and monthly repayment amount of equal principal and interest
for(int i=1;i<=month;i++){
double value = Math.pow((1+monthPrate),month+1);
rate2[i-1] = borrowMoney*(monthPrate*value)/(value-1);
}

return rate2;
}

//Total repayment amount for equal repayment
public static Double repaySame(){
rate2 = periodSameMonth();

//The total repayment amount and monthly repayment amount of equal principal and interest
for(int i=1;i<=month;i++){
amountRate2 += rate2[i-1];
}

return amountRate2;
}
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325906275&siteId=291194637