在java中为当前时间添加30分钟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9784614/
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
Add 30 minutes to the current time in java
提问by Pinakin
I want to get current time
in my application.
我想进入current time
我的应用程序。
Then i want to add 30 minutes
to the current time.
然后我想添加30 minutes
到当前时间。
What is the best practice to achieve this?
实现这一目标的最佳实践是什么?
I am able to get start and stop time from my web service.
eg: ((start time)11:00 am to (stop time) 11:00 pm)
now, i would like to add 30 minutes
to the current time till the stop time is reached.
我可以从我的 Web 服务中获取开始和停止时间。例如:((开始时间)11:00 am 到(停止时间)11:00 pm)现在,我想添加30 minutes
到当前时间直到到达停止时间。
采纳答案by Leo
Calendar now = Calendar.getInstance();
now.add(Calendar.MINUTE, 30);
And to output the time you could use
并输出您可以使用的时间
// 24 hours format
SimpleDateFormat df = new SimpleDateFormat("HH:mm");
// AM/PM format
SimpleDateFormat df = new SimpleDateFormat("hh:mm aa");
System.out.println(df.format(now.getTime()));
回答by Shahzad Imam
System.currentTimeMillis()+(30*60*1000);
回答by Matthew
Use the following:
使用以下内容:
//get current Time
long currentTime = System.currentTimeMillis();
//now add half an hour, 1 800 000 miliseconds = 30 minutes
long halfAnHourLater = currentTime + 1800000;