java 将分钟添加到当前时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14814703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:28:25  来源:igfitidea点击:

Adding minutes to current time

java

提问by Pavla Nováková

how do i add x amount of minutes to the current time?

我如何将 x 分钟数添加到当前时间?

as i currently have

正如我目前所拥有的

DateFormat dateFormat = new SimpleDateFormat("HH:mm");
        Calendar cal = Calendar.getInstance();
        System.out.println(dateFormat.format(cal.getTime()));
        return ((int) ((x) +      ));

which gets my time in HH:mm format, but what i want it to return is x minutes + current time, how would i return this ?

它以 HH:mm 格式获取我的时间,但我希望它返回的是 x 分钟 + 当前时间,我将如何返回它?

回答by partlov

If you have Calendarinstance it is easy:

如果你有Calendar实例,这很容易:

cal.add(Calendar.MINUTES, x);

回答by orique

Add this line before System.out.println

在此之前添加此行 System.out.println

cal.add(Calendar.MINUTE, x);

回答by Pavla Nováková

Joda DateTime libraryprovide a lot of common and usefull date/time manipulation methods. It is worth to look. In your case you can simply write:

Joda DateTime 库提供了很多常用和有用的日期/时间操作方法。值得一看。在你的情况下,你可以简单地写:

DateTime addTwoMinutes = DateTime.now().plusMinutes(2);

回答by user2061037

Just add the amount of time to cal before formatting it.

只需在格式化之前添加 cal 的时间量。

void    add(int field, int amount)

Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules.

根据日历的规则,向给定的日历字段添加或减去指定的时间量。

Field would be Calendar.MINUTE for example

例如,字段将是 Calendar.MINUTE

回答by Gereon

long now = System.currentTimeMillis();
return now + (minutes * 60 * 1000);