如何将时间添加到当前时间? [英] How to add time to the current time?
本文介绍了如何将时间添加到当前时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的初始时间为 12:00:00
,我需要在其中添加 144
分钟.
I have an initial time 12:00:00
and I need to add 144
mins to it.
预期输出为 15.24.00
(即,将初始时间增加2.24小时).
The expected output is 15.24.00
(i.e, adding 2.24 hours to the initial time).
我应该如何更新下面给出的当前代码?
How should I update the current code given below ?
String startTime = "13:00:00";
SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss");
Date date1 = null;
Date date2 = null;
try {
date1 = format.parse(startTime);
} catch (ParseException e) {
e.printStackTrace();
}
Long addition = (long) (TimeUnit.MILLISECONDS.toMinutes(date1.getTime()));
System.out.println("Difference : "+addition);
推荐答案
您应使用现代的日期时间API,如下所示:
You should use a modern date-time API as follows :
import java.time.Duration;
import java.time.LocalTime;
public class Main {
public static void main(String[] args) {
LocalTime time = LocalTime.parse("13:00:00").plus(Duration.ofMinutes(144));
System.out.println(time);
}
}
输出:
15:24
在此处中查看有关更多信息
这篇关于如何将时间添加到当前时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文