Reputation: 10746
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)
//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?
Upvotes: 0
Views: 63
Reputation: 14338
By System.currentTimeMillis() / 1000
you get seconds, not milliseconds. 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.
BTW, this doesn't handle possible daylight saving clock changes, but I believe it is not so important here.
Upvotes: 4