2016-03-17 35 views
1

我需要保持开始第一次迭代的开始日期常量,并将单位的结束日期添加,迭代应该发生,直到开始日期在结束数据之前。添加日期与恒定单位之间的差异

实施例:

开始时间(1/1/2015)和结束日期(12/31/2015)之间的差异为12个月

和单元4个月

然后我应该得到

Contract Start date  End date 
1   1/1/2015  4/31/2015 
2   5/1/2015  8/31/2015 
3   9/1/2015  12/31/2015 

CODE我已经试过:

private void contractDetails(List<PolicyPeriodFormulaType> policyPeriod){ 


    for (PolicyPeriodFormulaType policyPeriodFormulaType : policyPeriod) { 
     Date effectiveDateFrom = policyPeriodFormulaType.getEffectivePeriod().getEffectiveFrom(); 
     Date effectiveDateTo = policyPeriodFormulaType.getEffectivePeriod().getEffectiveTo(); 
     Instant effectiveFrom = effectiveDateFrom.toInstant(); 
     ZonedDateTime zdt = effectiveFrom.atZone(ZoneId.systemDefault()); 
     LocalDate fromDate = zdt.toLocalDate(); 

     Instant effectiveTo = effectiveDateTo.toInstant(); 
     ZonedDateTime zdt1 = effectiveTo.atZone(ZoneId.systemDefault()); 
     LocalDate toDate = zdt.toLocalDate(); 



     int unit = policyPeriodFormulaType.getUnits(); 
     String unitMeasurement = policyPeriodFormulaType.getUnitOfMeasurement(); 
     Period p = Period.between(fromDate,toDate); 
     int months = p.getMonths(); 
     int years = p.getYears(); 
     if(unitMeasurement.equalsIgnoreCase("Month")){ 
      months = months+(years*12); 
     } 
     int duration = months/unit; // 12/4 = 3 
     int i =0; 
     while(fromDate.isBefore(toDate)){ 
      fromDate=fromDate; 
      toDate=fromDate.plusMonths(unit); 
      fromDate=toDate.plusMonths(unit); 
     } 


    } 



} 
+0

@mikecat_mixc,你可以请告诉我,我要去哪里错了 –

+0

你是什么意思“你应该得到的”表你说你应该得到?你的代码不输出任何东西,所以我不知道你打算如何“获取”该表。如果你只是在谈论某些点的某些变量的值,那么你并没有告诉我们**在哪**点指出哪些**变量。 – ajb

+0

我应该得到的手段,预期的输出应该是像上面的表格。 –

回答

1

坦率地说,你应该使用某种形式的Java或者8的日期/时间API或乔达,时间或其他,例如...

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy"); 
LocalDate startDate = LocalDate.parse("01/01/2015", dtf); 
LocalDate endDate = LocalDate.parse("12/31/2015", dtf); 

LocalDate date = startDate; 

Period period = Period.parse("P4M"); 
// or 
//Period period = Period.ofMonths(4); 
// or 
//Period period = Period.of(0, 4, 0); 

while (date.isBefore(endDate)) { 
    LocalDate to = date.plus(period); 
    System.out.println(dtf.format(date) + " - " + dtf.format(to.minusDays(1))); 
    date = to; 
} 

它打印

01/01/2015 - 04/30/2015 
05/01/2015 - 08/31/2015 
09/01/2015 - 12/31/2015 

从这一点,它止跌创建一个容器类(即Contract样式类)以包含来源值的过程非常艰难。然后,您可以使用ComparableComparator,让他们从小到某种List或阵列

0

请参见下面的代码:

//fromDate->starting date, toDate->endDate 
int duration = months/unit; // 12/4 = 3 
int i =1; 
//printing data 
System.out.println("Contract Start date  End date") 
while(fromDate.isBefore(toDate)){ 
    //get current duration, from to end 
    LocalDate currentUnitEndDate = fromDate.plusMonths(unit); 
    // printing index, current time, end time for current phase. 
    System.out.println(i+" "+fromDate"  "+ currentUnitEndDate); 
    //increment from date to unit no of months. 
    fromDate=fromDate.plusMonths(unit); 
    i++; 
}