【发布时间】:2021-11-07 18:04:39
【问题描述】:
我正在尝试将给定的“持续时间”添加到当前时间。例如,
String runtime = "1h58m"
我正在尝试将其添加到当前时间,以便输出看起来像
The runtime of <movie name> is <runtime>.
<Name> will end at <calculatedDate>
其中计算的日期是系统当前时间,加上给定的运行时间。
这是我目前拥有的,但不知道如何将运行时添加到当前系统时间。
public void playMovie(Movie movie){
SimpleDateFormat df = new SimpleDateFormat("MM/dd/Y hh:mmaa");
Date date = new Date();
System.out.println("The runtime of "+name+" is "+runtime);
System.out.println(""+name+" will end at "+df.format(date));
【问题讨论】:
-
不要使用
Date,而是使用java.time,这样就很简单了。 -
我建议你不要使用
SimpleDateFormat和Date。这些类设计不良且过时,尤其是前者,尤其是出了名的麻烦。而是使用来自java.time, the modern Java date and time API 的ZonedDateTime和DateTimeFormatter。
标签: java datetime java-time duration