[英]Timestamp adds extra days
I get the current time in milliseconds like 我以毫秒为单位获取当前时间
(System.currentTimeMillis() / 1000)
in line I use it: 我使用它:
foodObj.setValue("expires",(System.currentTimeMillis() / 1000)+ONE_WEEK+"");
and add one or two weeks using static ints 并使用静态整数增加一两周
public static int TWO_WEEKS = 1209600000;
public static int ONE_WEEK = 604800000;
public static int ONE_DAY = 86400000;
When I try to later turn this into days it's ahead by I think 16 or 17 days (idk if it counts milliseconds of a day as a day) 当我稍后尝试将其转换为几天时,我认为应该提前16天或17天(如果将一天中的毫秒数作为一天,则为idk)
//keysValues.get("expires") contains the timestamp
Long exp= Long.parseLong(keysValues.get("expires"));
long days=TimeUnit.MILLISECONDS.toDays(exp)-16;//otherwise this is 23
Why is there the inconsistency in time? 为什么时间不一致? Is it a Long or String conversion thing?
是Long还是String转换的东西?
By System.currentTimeMillis() / 1000
you get seconds , not milliseconds . 通过
System.currentTimeMillis() / 1000
您可以得到秒 ,而不是毫秒 。 So to make you code work properly, you should either use proper constants: 因此,为了使代码正常工作,您应该使用适当的常量:
public static final int ONE_DAY = 24 * 60 * 60; // 86400, not 86.4M
public static final int ONE_WEEK = ONE_DAY * 7;
public static final int TWO_WEEKS = ONE_WEEK * 2;
// ...
long days = TimeUnit.SECONDS.toDays(exp)
or not divide by 1000. 或不除以1000。
BTW, this doesn't handle possible daylight saving clock changes, but I believe it is not so important here. 顺便说一句,这不能解决夏令时的更改,但是我认为在这里并不是那么重要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.