In that code above I want to transform a Date by the TimeZone of Server (GMT-02:00) to TimeZone from my Device (GMT-03:00).
But I Always have the same Date of the server. What I doing wrong?
TimeZone timeZoneServer = TimeZone.getTimeZone(timeZoneServerString);
Long time = new Long(Long.valueOf(timeInMilis));
Calendar calendarDateServer = Calendar.getInstance(timeZoneServer);
calendarDateServer.setTimeInMillis(time);
long miliServer = calendarDateServer.getTimeInMillis();
TimeZone timeZoneMeu = TimeZone.getDefault();
Calendar meuCalendario = new GregorianCalendar();
meuCalendario.setTimeZone(timeZoneMeu);
meuCalendario.setTimeInMillis(miliServer);
Date transformedDate = meuCalendario.getTime();
return transformedDate;
You’re assuming that a
Datehas a time zone to start with. It doesn’t. ACalendardoes, but aDateis just milliseconds since the Unix epoch. It doesn’t know about calendar systems or time zones. It’s just a point in time.It’s not clear what you want to do with the result – but if it’s a matter of formatting it for display, just use
SimpleDateFormatand set the time zone on that instead.I would also strongly recommend that you use Joda Time instead of the built-in types… it’s a much more sensible API.