将分钟转换为毫秒 Java/android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21852754/
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
Convert Minutes to milliseconds Java/android
提问by TheDevMan
I am trying to convert given number of minutes into milliseconds.
我正在尝试将给定的分钟数转换为毫秒。
For eg: 15mins or 20mins or 44mins should be converted to milliseconds programmatically.
例如:15 分钟或 20 分钟或 44 分钟应以编程方式转换为毫秒。
I tried the below:
我尝试了以下方法:
Calendar alarmCalendar = Calendar.getInstance();
alarmCalendar.set(Calendar.MINUTE,15);
long alarmTime = alarmCalendar.getTimeInMillis();
Log.e("Milli", "seconds"+alarmTime);
This doesn't give the right value? What is the best way to convert this?
这没有给出正确的值?转换它的最佳方法是什么?
采纳答案by Marco Bolis
回答by Marco Bolis
int minutes = 42;
long millis = minutes * 60 * 1000;
回答by Venkatesh S
1 minute = 60000 millisecs.
1 分钟 = 60000 毫秒。
int minutes = 1;
long milliseconds = minutes * 60000;
回答by JourneyMan
This gives you the time in milli.
这为您提供了以毫为单位的时间。
long currentTime = System.currentTimeMillis()
Handler handler = new Handler();
long waitTime = currentTime + (15*60*1000);
handler.postDelayed(new Runnable() {
public void run() {
// Alert or do something here.
}
}, waitTime);
This piece of code sleeps for 15 minutes and then executes the handler's run method. This way you can raise an alarm etc...
这段代码休眠 15 分钟,然后执行处理程序的 run 方法。这样你就可以发出警报等......
回答by ConnorWGarvey
In Java 8+, use java.time
.
在 Java 8+ 中,使用java.time
.
java.time.Duration.ofMinutes(15).toMillis();