2012-04-13 82 views
0

我有如下一個類DurationFormatter:錯誤與喬達日期時間格式化

import java.util.Date; 
import org.joda.time.DateTime; 
import org.joda.time.Period; 
import org.joda.time.format.PeriodFormatter; 
import org.joda.time.format.PeriodFormatterBuilder; 

public class DurationFormatter { 

    private final static PeriodFormatter DURATION_FORMATTER = 
    new PeriodFormatterBuilder().appendYears() 
           .appendSuffix("year", "years") 
           .appendSeparator(" ") 
           .appendMonths() 
           .appendSuffix("month", "months") 
           .appendSeparator(" ") 
           .appendDays() 
           .appendSuffix("day", "days") 
           .appendSeparator(" ") 
           .appendHours() 
           .appendSuffix("hour", "hours") 
           .appendSeparator(" ") 
           .appendMinutes() 
           .appendSuffix("minute", "minutes") 
           .appendSeparator(" ") 
           .appendSeconds() 
           .appendSuffix("second", "seconds") 
           .toFormatter(); 

    public static String format(Date start) { 
    StringBuffer result = new StringBuffer(); 
    DURATION_FORMATTER.printTo(result, 
           new Period(new DateTime(start), new DateTime())); 
    return result.toString(); 
    } 

    public static String format(Date start, Date end) { 
    StringBuffer result = new StringBuffer(); 
    DURATION_FORMATTER.printTo(result, 
           new Period(new DateTime(start), 
              end == null 
              ? new DateTime() 
              : new DateTime(end))); 
    return result.toString(); 
    } 

} 

這是我的單元測試:

import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 

import junit.framework.Assert; 

import org.joda.time.DateTime; 
import org.joda.time.Period; 
import org.junit.Test; 

public class DurationFormatterTest { 

    @Test 
    public void testFormatDate() throws ParseException { 

     int years = 0; 
     int months = 0; 
     int weeks = 0; 
     int days = 0; 
     int hours = 0; 
     int minutes = 0; 
     int seconds = 0; 
     SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); 
     String dateString = "07/27/2010 12:07:34"; 
     Date startDate = (Date) df.parse(dateString); 

     // Find duration 1 
     String duration1 = DurationFormatter.format(startDate); 

     // Parse duration 1 and set values into new Period 
     String[] tokens = duration1.split("[ ]"); 
     for(int index = 0; index < tokens.length; index++) { 
      String token = tokens[index]; 
      if(token.contains("years")) { 
       years = Integer.valueOf(token.replace("years", "")); 
       System.out.println("Years are: " + years); 
      } 
      else if(token.contains("months")) { 
       months = Integer.valueOf(token.replace("months", "")); 
       System.out.println("Months are: " + months); 
      } 
      else if(token.contains("days")) { 
       days = Integer.valueOf(token.replace("days", "")); 
       System.out.println("Days are: " + days); 
      } 
      else if(token.contains("hours")) { 
       hours = Integer.valueOf(token.replace("hours", "")); 
      } 
      else if(token.contains("minutes")) { 
       minutes = Integer.valueOf(token.replace("minutes", "")); 
      } 
      else if(token.contains("seconds")) { 
       seconds = Integer.valueOf(token.replace("seconds", "")); 
      } 
     } 

     Period period = new Period(years, months, weeks, days, hours, minutes, seconds, 0); 

     // User period to initialize new endDate 
     DateTime endDate = new DateTime(startDate).plus(period); 

     // Find duration 2 using new endDate 
     String duration2 = DurationFormatter.format(startDate, endDate.toDate()); 

     // If the durations are the same, then success. 
     Assert.assertEquals(
       "The date of " + duration2 
       + " is equal to " + duration1, 
       duration1, duration2); 
    } 
} 

結果總是輸出錯誤:

JUnit的。 framework.ComparisonFailure: 5天23小時5分23秒等於1個月5天23小時5分23秒預計:< [1個月] 5天23小時s 5分鐘...>但是:< [] 5天23小時5分鐘...>

字符串'[1month]'總是缺失。請檢查我是否在代碼中丟失了某些東西?

感謝

回答

1

在代碼中,你有.appendSuffix("month", "months")其中"month"是單數形式,"months"是複數形式。

您的測試只能通過解析複數形式:

else if(token.contains("months")) { 
    ... 
} 

在這種情況下,因爲只有1個月,因此單一測試失敗。

更新您的測試代碼來解析單數和複數形式,它應該工作!

Documentation