I have the requirement to calculate the price with two same dates but different input data formats. I am hoping for the same result. How can make the below-mentioned code so that output should be the same?
Sample code-calculation
-
String str="14-04-2021"; // Current date. SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy"); Date startDate = df.parse(str); output while pricing logic calculation = 43.06 -
Date date = new Date(); // System date. while calculating some price logic = 42.904
public double getCalculatedProductTotalPriceBasedOnRdd(double prdTotalPrice, String pPMAnnualNetPrice, String rdd) throws ParseException {
Date startDate;
if (null != rdd) {
//startDate=new Date();
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
startDate = df.parse(rdd);
} else {
startDate = new Date();
}
Calendar cal = Calendar.getInstance();
cal.setTime(startDate);
cal.set(Calendar.MILLISECOND, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.HOUR, 0);
Date sDate=cal.getTime();
int year = cal.get(Calendar.YEAR);
cal.set(Calendar.YEAR, year);
int days = calculateDaysBetweenTwoDates(startDate);
boolean leapYear = cal.getActualMaximum(Calendar.DAY_OF_YEAR) > 365;
if (null != pPMAnnualNetPrice) {
if (leapYear) {
prdTotalPrice = (Double.parseDouble(pPMAnnualNetPrice) * days) / 366;
} else {
prdTotalPrice = (Double.parseDouble(pPMAnnualNetPrice) * days) / 365;
}
}
return prdTotalPrice;
}