根据比例来设置时间区间。我想了几种方案,其中有一条是比较清晰易实现的。先获得一个时间段,然后根据各个阶段的比例来获得各个阶段所占据的时间。
使用Calendar这个类。
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟
Date contract= sdf.parse(contractTime);
Date delivery = sdf.parse(deliveryTime);
Calendar startcalendar = Calendar.getInstance();
Calendar endcalendar = Calendar.getInstance();
startcalendar.setTime(contract);
endcalendar.setTime(delivery);
long val = endcalendar.getTimeInMillis() - startcalendar.getTimeInMillis() ; long days = val / (1000 * 60 * 60 * 24);
val就是天数。也可以用自定义的方式得到天数int day = DateUtils.differentDays(contract, delivery);
double designLink0 = Double.valueOf(mStrategy.getDesignLink().split("%")[0])/100;
double processLink0 = Double.valueOf(mStrategy.getProcessLink().split("%")[0])/100;
double productionLink0 = Double.valueOf(mStrategy.getProductionLink().split("%")[0])/100;
以上是获得比例。
BigDecimal days = new BigDecimal(Double.toString(day));
BigDecimal designLink = new BigDecimal(Double.toString(designLink0));
BigDecimal processLink = new BigDecimal(Double.toString(processLink0));
BigDecimal productionLink = new BigDecimal(Double.toString(productionLink0));
将他们转成bigDecimal类型。
BigDecimal d1 = days.multiply(designLink).setScale(2);
获得天数,并保留两位小数。
BigDecimal day1 = BigDecimal.ZERO;
用BigDecimal.ZERO来获得初始值0.
BigDecimal d1Y = d1.divideAndRemainder(BigDecimal.valueOf(1))[1];
获得余数。
if(d1Y.compareTo(BigDecimal.ZERO) == 1){ //用余数和0作比较看看是否有余数,如果有余数占据一天BigDecimal flag3 = d3;
Date designPlanningStart = null;
Date designPlanningEnd = null;
Date processPlanningStart = null;
Date processPlanningEnd = null;
Date productionScheduleStart = null;
Date productionScheduleEnd = null;
double f1 = flag1.doubleValue();
if(designDays >0){ 如果这个区间有比例
if(f1 > 0 && f1 <1){ 对天数的累积作比较,看看是否超过一天,如果没超过就还在当天,如果超过就翻天。
designPlanningStart = transition;
designPlanningEnd = transition;
}else{
Calendar day01 = Calendar.getInstance();
day01.setTime(contract);
day01.set(Calendar.DATE,day01.get(Calendar.DATE)+designDays);
designPlanningStart = transition;
designPlanningEnd = day01.getTime();
transition = designPlanningEnd;
}
}
double f2 = flag2.doubleValue();
if(processDays >0){
if(f2 > 0 && f2 <1){
// 天数不更新
processPlanningStart = transition;
processPlanningEnd = transition;
}else{
processPlanningStart = transition;
Calendar day02 = Calendar.getInstance();
day02.setTime(contract);
day02.set(Calendar.DATE,day02.get(Calendar.DATE)+processDays);
processPlanningEnd = day02.getTime();
transition = processPlanningEnd;
}
}
double f3 = flag3.doubleValue();
if(productionDays >0){
if(f3 > 0 && f3 <1){
// 天数不更新
productionScheduleStart = transition;
productionScheduleEnd = transition;
}else{
productionScheduleStart = transition;
Calendar day03 = Calendar.getInstance();
day03.setTime(contract);
day03.set(Calendar.DATE,day03.get(Calendar.DATE)+productionDays);
productionScheduleEnd = day03.getTime();
transition = productionScheduleEnd;
}
}
A a = new A();
a.setDesignPlanningStart(designPlanningStart);
a.setDesignPlanningEnd(designPlanningEnd);
a.setProcessPlanningStart(processPlanningStart);
a.setProcessPlanningEnd(processPlanningEnd);
a.setProductionScheduleStart(productionScheduleStart);
a.setProductionScheduleEnd(productionScheduleEnd);
就可以得到他们所分配的时间。