Java Date Conversion from Millisecond time -
i trying calculate offset time given date. date string , parse it, , have offset in milliseconds. example
date: 2015-08-18 00:00:00
offset time: 2678400000, when applied date, equals 2015-09-18 00:00:00, 31 days later.
my goal store each of offsets (years/months/days/hours/minutes/seconds) in array, can use later. when run calculation using calendar class, i'm getting hours reason when called offsetconverter(2678400000)
output: 0 years 0 months 31 days 19 hours 0 minutes 0 seconds
here code found , modified link best way convert milliseconds number of years, months , days
public static int[] offsetconverter(long offset) { int[] delay = new int[6]; //delay 0-3 = years/months/days/seconds calendar c = calendar.getinstance(); c.settimeinmillis(offset); delay[0] = c.get(calendar.year) - 1970; delay[1] = c.get(calendar.month); delay[2] = c.get(calendar.day_of_month); delay[3] = c.get(calendar.hour_of_day); delay[4] = c.get(calendar.minute); delay[5] = c.get(calendar.second); (int = 0; i< delay.length; i++) system.out.print(delay[i] + " "); system.out.println(); return delay; }
if sees i'm doing wrong or has simpler way appreciate help. thanks!
milliseconds date#gettime()
arrive in utc timezone, instantiate calendar default timezone, local. adds hours result.
to solve issue, create calendar
instance using utc timezone:
calendar c = calendar.getinstance(timezone.gettimezone("utc"));
Comments
Post a Comment