English 中文(简体)
Error with Joda Time Formatter
原标题:Error with Joda DateTime Formatter

我有以下类别:

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: The date of 5days 23hours 5minutes 23seconds is equal to 1month 5days 23hours 5minutes 23seconds expected:<[1month ]5days 23hours 5minut...> but was:<[]5days 23hours 5minut...>

扼杀[1个月]总是失踪。 请检查一下我是否在法典中遗漏了东西?

成就

最佳回答

In your code, you have .appendSuffix("month", "months") where "month" is the singular form, "months" is the plural form.

Your test only parses the plural forms:

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

在此情况下,测试失败,因为它只有1个月,因此是独一无二的。

• 更新你的试验守则,使之包括单体和复方形式,并发挥作用!

Documentation

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签