0

In android, I download date information from a MySQL database on a free web server, then convert it to a Date object using:

Note: the server time is 5 hours ahead of toronto.

public static Date getDateFromSQLDate(String sqldate) {
    try {
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        Date date = (Date) formatter.parse(sqldate);

        TimeZone targetTimeZone = TimeZone.getDefault();
        TimeZone serverTimeZone = TimeZone.getTimeZone("Europe/London");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.setTimeZone(serverTimeZone);


        calendar.add(Calendar.MILLISECOND, serverTimeZone.getRawOffset() * -1);
        if (serverTimeZone.inDaylightTime(calendar.getTime())) {
            calendar.add(Calendar.MILLISECOND, calendar.getTimeZone().getDSTSavings() * -1);
        }

        calendar.add(Calendar.MILLISECOND, targetTimeZone.getRawOffset());
        if (targetTimeZone.inDaylightTime(calendar.getTime())) {
            calendar.add(Calendar.MILLISECOND, targetTimeZone.getDSTSavings());
        }

        return calendar.getTime();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;
}

This doesn't seem to work..

The problem is that the date is relative to the timezone of the server. The one downloading could be confused with the times as they don't know its not in their own timezone.

I have a Date object, is there a way I can retrieve the timezone of the location the users phone is in, and then modify that Date object to be their own timezone?

Thanks

EDIT:

How to get TimeZone from android mobile?

This gets a timezone object, but how do I change a Date object with it?

Community
  • 1
  • 1
omega
  • 40,311
  • 81
  • 251
  • 474

3 Answers3

2

My app has to deal with server time similar to you. (All datetime that I got from server represent datetime at UTC +00:00)

// date string to convert
String dateString = "2014-01-07 12:00:00"

// create date formatter, set time zone to UTC and parse 
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
TimeZone serverTimeZone = TimeZone.getTimeZone("UTC");
formatter.setTimeZone(serverTimeZone);
Date date = formatter.parse(dateString);

Log.i("Debug", "date object : " + date.toString());
// I'm in Bangkok (UTC +07:00) so I'll see "Wed Jan 07 19:00:00 GMT+07:00 2015"
// If you do this in Toronto, you should see "Wed Jan 07 07:00:00 GMT -05:00 2015"

When you wanna print this date in Toronto, I believe you don't have to calculate DST by yourself because calendar and date formatter should handle that (not sure, I read from somewhere long ago)

// Create timezone for Toronto
TimeZone torontoTimeZone = TimeZone.getTimeZone("America/Toronto");

// Create calendar, set timezone, to see hour of day in Toronto
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(torontoTimeZone);
calendar.setTime(date);

Log.i("Debug", "Hour of day : " + calendar.get(Calendar.HOUR_OF_DAY);
// Hour of day : 7

// Create date formatter, set timezone, to print date for Toronto user.
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy, hh:mm", Locale.US);
formatter.setTimeZone(torontoTimeZone);
String torontoDate = formatter.format(date);

Log.i("Debug", "Date in Toronto : " + torontoDate);
// Date in Toronto : 07 Jan 2015, 07:00

You can set calendar and date formatter to user timezone by replace

TimeZone timezone = TimeZone.getTimeZone("America/Toronto")

with

TimeZone timezone = TimeZone.getDefault()

When I deal with date from server, I'll

  • request UTC time from server, if server doesn't send me UTC time, convert to UTC time first.
  • when parse date object from server, I always create date object represent time at UTC (time at server)
  • perform calculation or anything else with UTC date object.
  • pass only UTC date object from and to Activity/Fragment/Service/Model
  • only format date string with user timezone only when I need to display to user.
Tar_Tw45
  • 3,122
  • 6
  • 35
  • 58
0

This is how you can change the date to your timezone

SimpleDateformat sdf = new SimpleDateFormat("yourformat");   
TimeZone tz = TimeZone.getDefault(); 
sdf.setTimezone(tz);
sdf.format(yourdate); //will return a string in "your-format" to represent date
Gaurav
  • 1,965
  • 2
  • 16
  • 32
0

You're on the right track. You need to set the time zone of the Calendar object to the server's time zone. Then you can add the offset (and factor in DST) with the TimeZone that you got from the user's device (the link you included).

Code:

calendar.add(Calendar.MILLISECOND, serverTimeZone.getRawOffset() * -1);
        if (serverTimeZone.inDaylightTime(calendar.getTime())) {
            calendar.add(Calendar.MILLISECOND, calendar.getTimeZone().getDSTSavings() * -1);
        }

        calendar.add(Calendar.MILLISECOND, targetTimeZone.getRawOffset());
        if (targetTimeZone.inDaylightTime(calendar.getTime())) {
            calendar.add(Calendar.MILLISECOND, targetTimeZone.getDSTSavings());
        }

After that, you can retrieve the date in the usual way from the Calendar. Also see this answer for more information.

Community
  • 1
  • 1
Aaron D
  • 7,540
  • 3
  • 44
  • 48
  • Can you show the complete code, i'm not sure what I'm supposed to keep from my code. – omega Jan 07 '15 at 02:48
  • im not sure if it is really working, because it gave correct times when I had `calendar.setTimeZone(targetTimeZone);` and also when I changed it to `calendar.setTimeZone(serverTimeZone);`. – omega Jan 07 '15 at 03:14