简体   繁体   English

如何使用 LocalTime 在 Java 中求和两次?

[英]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).我想以(小时:分钟)的格式获取两次(12:30 + 8:30 = 21:00)的总和。

Any other suggestions?还有其他建议吗?

You are trying to add two LocalTime variables. 您正在尝试添加两个LocalTime变量。 This is wrong as a concept. 这是一个错误的概念。 Your time2 should not be a LocalTime , it should be a Duration . 你的time2不应该是LocalTime ,它应该是一个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. 相反,重构您的代码,以便time2首先是一个Duration

The answer of Mike Nakis does not true. Mike Nakis的回答并非如此。

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. 我已经确认并只的Java 8已经LocalTime.of所以迈克Nakis的回答是完全正确的。 Please see his answer. 请看他的回答。

[This section still keep. [此部分仍然保留。 in case LocalTime in joda library ] LocalTime在joda库中的LocalTime]

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. 在约达- 时间段表示在一段时间中的字段来定义,例如,3年5月2天及7小时。 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. 例如,考虑1年的时间,如果我们将其添加到1月1日,我们将始终在1月1日到达,但持续时间将取决于干预年是否是闰年。

LocalTime is an immutable time class representing a time without a time zone . LocalTime是一个不可变的时间类,表示没有时区的时间 So, base on above definition, period is suitable for you adding time to LocalTime . 因此,基于上面的定义,句点适合于为LocalTime添加时间。 In fact, API has proved this: 事实上,API证明了这一点:

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() 你可以使用方法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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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