0

I wrote a simple java program

System.out.println(TimeZone.getDefault().getID()); // printing Asia/Calcutta

Same line of code in Android file:

System.out.println(TimeZone.getDefault().getID()); //printing America/New_York

My default time zone is Asia/Calcutta, why is this happening?
Please help.

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
Paritosh Ahuja
  • 1,239
  • 2
  • 10
  • 19

2 Answers2

0

In Java TimeZone.getDefault() is initialized that way:

  1. Look up the system property "user.timezone" (this is always a good work-around to start your application with this property set to "Asia/Kolkata").

  2. If not present then look at "user.country" and "java.home" and use native method for accessing platform zone information (so you should really check your android-OS-configuration for timezone).

  3. If still not present then fall back to GMT.

UPDATE:

For Android (which is not really Java) you can also check this other SO-post where an answer mentions the system property System.getProperty("persist.sys.timezone");. You can try to print out this property for you and follow given links there.

Community
  • 1
  • 1
Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
  • user.timezone and user.country how to see these properties. – Paritosh Ahuja Apr 07 '14 at 16:58
  • @ParitoshAhuja Look at `System.getProperty("user.timezone");` (normally yielding `null` unless you have explicitly started your application with this property), but your main task should be checking your OS-settings for timezone. – Meno Hochschild Apr 07 '14 at 17:01
  • I don't want to hard code the value, i want to fetch the timezone specific to that location and then use it. – Paritosh Ahuja Apr 08 '14 at 09:05
  • @ParitoshAhuja Keep in mind that in many important cases one given location has more than one timezone (examples: US, Russia etc.). – Meno Hochschild Apr 08 '14 at 09:22
-1

You can Always use :

TimeZone tz = Calendar.getInstance().getTimeZone();
ExceedLimits
  • 141
  • 3