java - Android date with added milliseconds not at the correct time -


what trying store date, represented long of milliseconds, next midnight current time. so, posting @ 10:11 pm, want date representing 12:00 tomorrow morning. task, wrote line of code (knowing there 86400000 milliseconds in 1 day):

long time = system.currenttimemillis() + (86400000 - (system.currenttimemillis() % 86400000)); 

the line designed calculate milliseconds last midnight, substract 1 whole day find time until next midnight, add current time new value value of next midnight. whatever reason though, date object using debug spits out "wed apr 20 20:00:00 edt 2016" when calling #tostring() method. current time said "tue apr 19 22:08:34 edt 2016" @ same time other date being calculated.

this means long of milliseconds representing 8:00 pm next day, while want represent 12:00 am. can me spot flaw in logic?

i missing obvious, bear me.

note: tried calculating time this:

long time = system.currenttimemillis() - (system.currenttimemillis() % 86400000) + 86400000; 

but resulted in same date object.

you forgetting adjust timezone. here simple way achieve this.

timezone tz = timezone.getdefault(); long time = system.currenttimemillis() + (86400000 - (system.currenttimemillis() % 86400000)); time -= tz.getoffset(time);  system.out.println(new date(time)); 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -