Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7784925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:10:24+00:00 2026-06-01T20:10:24+00:00

I have a class DurationFormatter as below: import java.util.Date; import org.joda.time.DateTime; import org.joda.time.Period; import

  • 0

I have a class DurationFormatter as below:

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

}

And this is my Unit Test:

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

The result always output with error:

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…>

The string ‘[1month ]’ always missing. Please check if I’m missing something in code?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:10:25+00:00Added an answer on June 1, 2026 at 8:10 pm

    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") ) {
        ...
    }
    

    In this case, the test fails because it is only 1 month and therefore singular.

    Update your testing code to parse both singular and plural form and it should work!

    Documentation

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class: public class MyClass { [XmlElement(Date)] public DateTime Date { get; set;
I have class class DateOptTimeType implements org.hibernate.usertype.UserType that works with two columns @org.hibernate.annotations.Type(type =
I have class like below template<class T> class Student { public: static Student& Instance();
I have class A, which exposes an event. an object of class B subscribed
I have class A: public B { ...} vector<A*> v; I want to do
I have class Fred { public: void inspect() const {}; void modify(){}; }; int
I have class that represents users. Users are divided into two groups with different
i have class named Group i tried to test configuration: var cfg = new
I have: class Foo; class Bar { Foo foo; Bar(): foo(foo) {}; } Bar
I have class A {} class B : A {} I also have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.