1

I've been having some problems with a time-based application I'm developing, which uses Java in the Android client side and Python in the server side. I encountered a discrepancy of -3 hours (which is exactly the timezone from the Android device) in the times reported by Java and Python, even though in both sides I'm getting the time in a way that (according to SO and other sites) returns a UTC time. In my client, I'm using

Date utcNow = new Date()

and in the server, i use

datetime.datetime.now()

Yet, when run at the same time, they yield 2013-09-09 11:52:16 and 2013-09-09 14:52:16, respectively. I suspect the issue is on Java side, since running date in the server returns the same value as Python (obviously), with the timezone UTC, and the same command in the Android device returns a BRT (GMT-3) time.

While searching for timezone adjustment on both languages, all answers claimed that the methods above would return a UTC date, yet this difference is clearly visible.

How can I convert the Android device's time to UTC?

Right now I'm using a really ugly (and extremely deprecated) solution: utcNow.setHours(utcNoe.getHours()+3)

EDIT: Side note: From Jon's answer, I noticed Java's Date objects are equivalent to Python's naive datetime objects.

5
  • How exactly are you determining the value of each expression? After all, a Date isn't a String... if you call Date.toString() that will show you the value in the system local time zone but that isn't information which is in the Date value itself. Commented Sep 9, 2013 at 15:08
  • The issue is creating the Dates in client side. This is more a conceptual problem than a programming language problem. Imagine the server is configured with UTC 0 and I use a client from Peru (where I live) with UTC -5 and send a request to the server with a Date created using my time zone. Commented Sep 9, 2013 at 15:08
  • @LuiggiMendoza: No, it's really not. A Date doesn't have a time zone. It's just an instant in time. Commented Sep 9, 2013 at 15:09
  • @JonSkeet I'm using a SimpleDateFormat. Commented Sep 9, 2013 at 15:10
  • 2
    @Kroltan: And are you specifying the time zone for that SimpleDateFormat? If not, that's the problem. Commented Sep 9, 2013 at 15:11

2 Answers 2

8

I strongly suspect the problem is in the code you haven't shown us - when you convert the Date to a String in Java via SimpleDateFormat.

A Date itself is just an instant in time, with no time zone or calendar system associated with it. In order to convert it to a string, both need to be applied - and the default time zone in a SimpleDateFormat is the system local time zone.

If you just change your code to:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                                               Locale.US);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateText = format.format(date);

then I suspect your problems will go away. Note that this is not part of the Date information itself. (It's really important to understand that.)

Also note that the date/time API in Java is pretty horrible. If you're able to use Joda-Time instead, you're likely to have a better time of it. (It would be overkill if all you need is a timestamp, but if you have any actual date/time work to do, you should definitely consider it.)

Sign up to request clarification or add additional context in comments.

3 Comments

I'm not on my work computer right now, so I can't test it, but this answer is really promising, I was just creating a SimpleDateFormat with a string format. It's weird to have timezones controlled by a format, not the date/time information itself. Indeed I only need to transmit a timestamp to the server once, to receive a list of new content, so Joda would just boost my APK size.
@Kroltan: It's not particularly weird - there are various different kinds of value that you may want, which is why Joda Time has Instant, LocalDateTime, LocalDate, LocalTime, DateTime etc.
As Jon Skeet suggests, Joda-Time is the way to go. To get a UTC date-time object: DateTime.now( DateTimeZone.UTC ). Unlike a java.util.Date, a Joda-Time DateTime object does have an assigned time zone.
1

This has been asked many times before. Have a look at Set Time Zone. You can also use this approach to change the timezone.

--- EDIT If you want to transmit your timezone as a string, you should transmit it using ISO_8601 that way you can adjust the time on your server.

1 Comment

The ISO 8601 standard is very useful. Unlike many standards, it is practical and well thought-out. Full of good stuff like an official definition of week-of-year, and representation of spans of time. The Wikipedia page is quite informative.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.