我正在尝试将毫秒日期转换为数字years
months
weeks
and days
.
例如:5 months, 2 weeks and 3 days
or 1 year and 1 day
.
我不想要:7 days
or 4 weeks
> 这应该是1 week
and 1 month
.
我尝试了几种方法,但它总是变成这样7 days and 0 weeks
.
My code:
int weeks = (int) Math.abs(timeInMillis / (24 * 60 * 60 * 1000 * 7));
int days = (int) timeInMillis / (24 * 60 * 60 * 1000)+1);
我必须将天数加 1,因为如果我有 23 小时,那么它应该是 1 天。
请解释如何正确转换它,我认为有更有效的方法来做到这一点。
我总是用它来从毫秒中获取年等,反之亦然。到目前为止我还没有遇到任何问题。希望能帮助到你。
import java.util.Calendar;
Calendar c = Calendar.getInstance();
//Set time in milliseconds
c.setTimeInMillis(milliseconds);
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
int hr = c.get(Calendar.HOUR);
int min = c.get(Calendar.MINUTE);
int sec = c.get(Calendar.SECOND);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)