根据时间比例设置时间区间

根据比例来设置时间区间。我想了几种方案,其中有一条是比较清晰易实现的。先获得一个时间段,然后根据各个阶段的比例来获得各个阶段所占据的时间。

使用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作比较看看是否有余数,如果有余数占据一天
day1 = BigDecimal.valueOf(1);
}
int designDays  = (int) d1.subtract(d1Y).add(day1).doubleValue(); 获得第一阶段的时间
BigDecimal flag1 = d1; //用flag来作为天数的累积。

BigDecimal d2 = days.multiply(processLink).setScale(2);
d2 = d2.add(d1);
BigDecimal day2 = BigDecimal.ZERO;
BigDecimal d2Y = d2.divideAndRemainder(BigDecimal.valueOf(1))[1];
if(d2Y.compareTo(BigDecimal.ZERO) == 1){
day2 = BigDecimal.valueOf(1);
}
int processDays = (int) d2.subtract(d2Y).add(day2).doubleValue();
BigDecimal flag2 = d2;

BigDecimal d3 = days.multiply(productionLink).setScale(2);
d3 = d3.add(d2);
BigDecimal day3 = BigDecimal.ZERO;
BigDecimal d3Y = d3.divideAndRemainder(BigDecimal.valueOf(1))[1];
if(d3Y.compareTo(BigDecimal.ZERO) == 1){
day3 = BigDecimal.valueOf(1);
}
int productionDays = (int) d3.subtract(d3Y).add(day3).doubleValue();

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);

就可以得到他们所分配的时间。

你可能感兴趣的:(根据时间比例设置时间区间)