Handling Hours with Joda Time

Asked

Viewed 471 times

0

 String tempo1 = "01:30:30";
 String tempo2 = "24:25:10";

    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    try {
        Date date1 = (Date)sdf.parse(tempo1);
        Date date2 = (Date)sdf.parse(tempo2);

        DateTime dateTime1 = new DateTime(date1);
        DateTime dateTime2 = new DateTime(date2);

Guys, how do I add these two strings together that look like this: "25:55:40"?

It’s always returning 1:55:40.

  • I don’t think it’s duplicate @Diegof because the question refers to the sum of times using Jodatime and not pure Java.

1 answer

0

Using Jodatime I believe that the following function would solve your problem:

private DateTime somarTempos(DateTime t1, DateTime t2) {
    return t1.plusHours(t2.getHourOfDay())
            .plusMinutes(t2.getMinuteOfHour())
            .plusSeconds(t2.getSecondOfMinute());
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.