4

Example:

LocalTime time1 = LocalTime.of(12, 30);
LocalTime time2 = LocalTime.of(8, 30);
time1 + time2   // Doesn't work.
time1.plus(time2)   // Doesn't work.

I want to get the sum of the two times (12:30 + 8:30 = 21:00) in the format of (hours:minutes).

Any other suggestions?

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Valio
  • 41
  • 1
  • 4

5 Answers5

11

You are trying to add two LocalTime variables. This is wrong as a concept. Your time2 should not be a LocalTime, it should be a Duration. A duration added to a time gives you another time. A time subtracted from a time gives you a duration. It is all nice and logical. Adding two times together is not.

It is possible with some hacking to convert your time to a duration, but I would strongly advise against that. Instead, restructure your code so that time2 is a Duration in the first place.

hqt
  • 29,632
  • 51
  • 171
  • 250
Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
2

You can do the following...

LocalTime t1 = LocalTime.of(9, 0);  // 09:00
LocalTime t2 = LocalTime.of(2, 30); // 02:30
LocalTime total = t1.plusHours(t2.getHour())
                    .plusMinutes(t2.getMinute());  // 11:30
1

The answer of Mike Nakis does not true.

The above sentence of mine doesn't true. I have checked and only Java 8 has LocalTime.of so Mike Nakis's answer is perfectly true. Please see his answer.

[This section still keep. in case LocalTime in joda library ]

I will explain:

A duration in Joda-Time represents a duration of time measured in milliseconds. The duration is often obtained from an interval. i.e. we can subtract start from end of an interval to derive a duration.

A period in Joda-Time represents a period of time defined in terms of fields, for example, 3 years 5 months 2 days and 7 hours. This differs from a duration in that it is inexact in terms of milliseconds. A period can only be resolved to an exact number of milliseconds by specifying the instant (including chronology and time zone) it is relative to. e.g. consider the period of 1 year, if we add this to January 1st we will always arrive at the next January 1st but the duration will depend on whether the intervening year is a leap year or not.

LocalTime is an immutable time class representing a time without a time zone. So, base on above definition, period is suitable for you adding time to LocalTime. In fact, API has proved this:

LocalTime localTime = new LocalTime(10, 30);
Duration d = new Duration(1, 0);
Period p = new Period(1, 0);
LocalTime newLocalTime = localTime.plus(d); // COMPILE ERROR
LocalTime newLocalTime = localTime.plus(p); // SUCCESS
hqt
  • 29,632
  • 51
  • 171
  • 250
  • 1
    @MikeNakis I'm sorry for all. I have upvoted again your answer (that I need edit so I can upvote again, haha). I'm sorry that I don't see carefully original question. – hqt Oct 04 '15 at 18:13
0

you can use the method sum()

LocalTime time1 = LocalTime.of(12, 30);
LocalTime time2 = LocalTime.of(8, 30);
Integer hour1 = time1.getHour();
Integer hour2 = time2.getHour();
Integer minute1 = time1.getMinute();
Integer minute2 = time2.getMinute();

Integer first = Integer.sum(hour1,hour2);
Integer second = Integer.sum(minute1,minute2);

System.out.println("The time is "+ first + " : " + second);

It must work

RoyalUp
  • 137
  • 1
  • 11
0
LocalTime t1 = LocalTime.parse('03:33:24')
LocalTime t2 = LocalTime.parse('03:13:41')

t1.plusHours(t2.hour)
  .plusMinutes(t2.minute)
  .plusSeconds(t2.second)
  .plusNanos(t2.nano)
Marcus Voltolim
  • 413
  • 4
  • 12