2016-03-17 38 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++; 
}