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

  • Home
  • SEARCH
  • 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 8192661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:13:44+00:00 2026-06-07T04:13:44+00:00

I have the following method to sum time: public static String sumTime(String date1, String

  • 0

I have the following method to sum time:

public static String sumTime(String date1, String date2) throws ParseException {
    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss.SSS");

    Date d1 = formatter.parse(date1);
    Date d2 = formatter.parse(date2);
    calendar.setTime(d2);

    d1 = DateUtils.addHours(d1, calendar.get(Calendar.HOUR_OF_DAY));
    d1 = DateUtils.addMinutes(d1, calendar.get(Calendar.MINUTE));
    d1 = DateUtils.addSeconds(d1, calendar.get(Calendar.SECOND));
    d1 = DateUtils.addMilliseconds(d1, calendar.get(Calendar.MILLISECOND));

    return formatter.format(d1);
}

DateUtils is from Apache Commons Lang 3
It works quite well for what I want, unless the sum is bigger than 24 hours.

For example:

String time = "00:00:00.000";

try {
    for (int i = 0; i < 24; i++) {
            time = sumTime(time, "01:00:00.123");
    }

    System.out.println(time);
} catch (ParseException e) {
    e.printStackTrace();
}

The result is:

00:00:02.952

But this is what I’d like it to be:

24:00:02.952

Is there any (easy) way to accomplish that?
I don’t mind using different libraries/methods, as long as I get the correct result.

Keep in mind that time will always start in 00:00:00.000;

  • 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-07T04:13:45+00:00Added an answer on June 7, 2026 at 4:13 am

    Date is not the right thing class to use. Date is a instant of time, not a “Date Difference”.

    The right thing to do will be to use a library like Joda Time as someone has already suggested. If you don’t want to do so – here’s a possible alternative:

    Parse the string into hours, minutes and seconds yourself, and then add it yourself.

    I would encourage you to look into a “well accepted” library though. There may be things I’m not thinking of in my solution. Also, you have add all the error checking.

    Here’s the starter code:

    public class TimeInterval {
    short milliSeconds;
    short seconds;
    short minutes;
    int hours;
    
    public TimeInterval (String dateString) {
        // HHHHHH:MI:SS.SSS
        Pattern pattern = Pattern.compile("(\\d+):(\\d\\d):(\\d\\d)\\.(\\d\\d\\d)");
        Matcher matcher = pattern.matcher(dateString);
        if ( matcher.find() ) {
            hours = Integer.parseInt(dateString.substring(matcher.start(1), matcher.end(1)));
            minutes = Short.parseShort(dateString.substring(matcher.start(2), matcher.end(2)));
            seconds = Short.parseShort(dateString.substring(matcher.start(3), matcher.end(3)));
            milliSeconds = Short.parseShort(dateString.substring(matcher.start(4), matcher.end(4)));
        }
    }
    
    private TimeInterval() {
    }
    
    public TimeInterval add(TimeInterval interval) {
        TimeInterval ret = new TimeInterval();
        ret.milliSeconds = (short) ((interval.milliSeconds + milliSeconds)%1000);
        int carry = (interval.milliSeconds + milliSeconds)/1000;
        ret.seconds = (short) ((interval.seconds + seconds)%60 + carry );
        carry =(interval.seconds + seconds)/60;
        ret.minutes = (short) ((interval.minutes + minutes)%60 + carry);
        carry = (interval.minutes + minutes)/60;
        ret.hours = (interval.hours + hours + carry);
        return ret;
    }
    
    @Override
    public String toString() {
        return String.format("%d:%02d:%02d.%03d", hours, minutes, seconds, milliSeconds);
    }
    }
    

    Using this class your program will be like :

    TimeInterval time = new TimeInterval("00:00:00.000");
    
    try {
        for (int i = 0; i < 24; i++) {
          time = time.add(new TimeInterval("01:00:00.123"));
        }
    
        System.out.println(time.toString());
    } catch (ParseException e) {
        e.printStackTrace();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following method: public static T ExecuteScalar<T>( string query, SqlConnection connection, params
I have the following method: public string Phase(string phase) { return Phase 1; }
I have the following method: public MethodInfo FancyGetMethodInfo (object obj, string methodName, Type[] methodSignature)
I have following method in wcf webenabled service Public Person AddPerson(Person p); As of
I have the following Interview question: class someClass { int sum=0; public void foo()
I have following method which I am using to load ActiveX control dynamically, Dim
I have the following method, which takes in the name of a file as
I have the following method that returns void and I need to use it
I have the following method where I want to test the event.status property only
I have the following method in my UIButton class: - (id)initWithCoder:(NSCoder *)aDecoder{ self =

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.