简体   繁体   中英

How to sum two times in Java, using LocalTime?

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?

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.

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. ie 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. eg 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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM