How to convert UTC timestamp to device local time in android

cavallo picture cavallo · Feb 13, 2013 · Viewed 99.8k times · Source

I need to convert the UTC time stamp that i get from the server to local device time. currently i get 5 hrs difference in my time. for example when i post to server the post time says 5 hours ago instead of a second ago. How to fix this issue. thanks

Below is the code that i do

long timestamp = cursor.getLong(columnIndex);
            CharSequence relTime = DateUtils
                    .getRelativeTimeSpanString(timestamp * 1000
                            + TimeZone.getDefault().getRawOffset(),
                            System.currentTimeMillis(),
                            DateUtils.MINUTE_IN_MILLIS);
            ((TextView) view).setText(relTime);

Answer

prgDevelop picture prgDevelop · Mar 27, 2013
int offset = TimeZone.getDefault().getRawOffset() + TimeZone.getDefault().getDSTSavings();
long now = System.currentTimeMillis() + offset;