java 如何将毫秒转换为天?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28797850/
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
How to convert milliseconds into days?
提问by Apurva
I need number of days from milliseconds.
我需要从毫秒开始的天数。
I am doing as,
我正在做,
long days = (millis / (60*60*24*1000)) % 365;
Is this true? If no please tell me how get number of days from milliseconds.
这是真的?如果不是,请告诉我如何从毫秒中获取天数。
Please don't suggestto do
请不要建议这样做
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(millis);
int mDay = calendar.get(Calendar.DAY_OF_MONTH);
回答by Trynkiewicz Mariusz
int days = TimeUnit.MILLISECONDS.toDays(miliseconds);
回答by Gabriele Mariotti
long days = (millis / (60*60*24*1000))
回答by David Crosby
Pretty sure that's correct, but without the modulo.
很确定这是正确的,但没有模数。
% 365 means divide it by 365 and get the remainder.
% 365 表示除以 365 得到余数。
There are (60*60*24*1000) millisecond in a day.
一天有 (60*60*24*1000) 毫秒。
So for conversion:
所以对于转换:
millis/(60 seconds * 60 minutes * 24 hours * 1000 ms/second)
should do it.
应该这样做。
回答by divya shree
One that works perfectly fine, but do check the server's standard time. Millisecond in general creates issue or rather mismatch when server's standard time is not what you expect it to be. Code:
一个工作得很好,但要检查服务器的标准时间。当服务器的标准时间不是您期望的那样时,毫秒通常会产生问题或不匹配。代码:
public boolean isAdult(Date userDob, int minimumAge) {
Calendar cal = Calendar.getInstance();
long dayNow = TimeUnit.MILLISECONDS.toDays(cal.getTimeInMillis());
cal.setTime(userDob);
long dayDob = TimeUnit.MILLISECONDS.toDays(cal.getTimeInMillis());
long ageInDays = dayNow - dayDob;
long ageInYears = ageInDays / 365;
if (ageInYears < minimumAge) {
return false;
} else {
return true;
}
}