Convert UTC time to local time java Android -
im retrieving values json service , retrieved datetime values stored in utc format. i've tried lot of sample codes convert datetime values user local timezone im still getting same value after conversion.
this have actually: (copied other posts)
string sjsondate = "2015-07-08t12:08:13.0625+00:00"; simpledateformat simpledateformat = new simpledateformat("yyyy-mm-dd't'hh:mm:ss"); simpledateformat.settimezone(timezone.gettimezone("utc")); try { date localdatetime = simpledateformat.parse(sjsondate); } catch (parseexception e) { e.printstacktrace(); }
the result value (localdatetime) same original value. in paraguay (gmt-4) , resulting values needs minus 1 hour diference, this: ("2015-07-08 07:13:25") (the values stored in argentina)
help please!
i've found solution, using day light savings had disccount 1 hour resulting datetime.
so, share code else:
public date getdateintimezone(date currentdate, string timezoneid) { timezone timezone = timezone.gettimezone(timezoneid); date localdatetime = new date(currentdate.gettime() + timezone.getoffset(currentdate.gettime())); calendar cal = calendar.getinstance(); cal.settimeinmillis(localdatetime.gettime()); if (timezone.usedaylighttime()) { // time zone uses daylight saving cal.add(calendar.millisecond, timezone.getdstsavings() * -1);// in milliseconds } return cal.gettime(); }
usage:
string sdate = "2015-07-08t12:08:13.0625+00:00"; try { simpledateformat simpledateformat = new simpledateformat("yyyy-mm-dd't'hh:mm:ss"); date thedate = simpledateformat.parse(sdate); date localdatetime = getdateintimezone(thedate, timezone.getdefault().getid()); } catch (parseexception e) { e.printstacktrace(); }
Comments
Post a Comment