JustWork
JustWork

Reputation: 1964

Android UTC Time Issue (I get incorrect time)

I want to subtract two dates in Android project.

When I use the statement:

DateTime now = new DateTime(DateTimeZone.UTC);

It gives 2014-03-17T12:49:06.670Z value instead of 2014-03-17T14:49:06.670Z (this is my current time on Android device)

When I convert this DateTime (2014-03-17T12:30:08.673+02:00) to UTC Time (2014-03-17T10:30:08.673Z) it gives the correct result but not for DateTime now = new DateTime(DateTimeZone.UTC);

What is wrong with new DateTime(DateTimeZone.UTC);?

Any help would be appreciated.

Upvotes: 1

Views: 890

Answers (2)

Israel Varea
Israel Varea

Reputation: 2630

Try getting your local time this way:

DateTime now = new DateTime();

This time is "your current time on Android device". More precisely DateTime calculates its fields with respect to a time zone. It means it will return UTC+2 if you are located in Eastern European Countries (Winter Time) or Wester European Countries (Summer time).

Upvotes: 1

Meno Hochschild
Meno Hochschild

Reputation: 44061

On my PC nothing is wrong with new DateTime(DateTimeZone.UTC). The type DateTime internally stores the given time zone (here UTC) and uses it for the output of its method toString(), hence the UTC-string-format and not another alternative format with offset = +02:00.

Upvotes: 0

Related Questions