1

The question seems quite simple but I would really appreciate the help. Search the internet many times.

Following is the code to get current timezone of Android Devices in GMT:

Java.Util.Calendar mCalendar = new Java.Util.GregorianCalendar();
Java.Util.TimeZone mTimeZone = mCalendar.TimeZone;
int mGMTOffset = mTimeZone.RawOffset + (mTimeZone.InDaylightTime(new Java.Util.Date()) ? mTimeZone.DSTSavings : 0);

string timeDiff = Java.Util.Concurrent.TimeUnit.Hours.Convert(mGMTOffset, Java.Util.Concurrent.TimeUnit.Milliseconds).ToString();

Outputs:

If Mobile time is GMT-03:00 Then timeDiff = -3

If Mobile time is GMT+00:00 Then timeDiff = 0

If Mobile time is GMT+05:00 Then timeDiff = 5

If Mobile time is GMT+05:45 Then timeDiff = 5 (In Correct)

Issue comes when GMT is in decimal values like 5:45 shows 5.

What should I do in these types of scenarios?

I took help from the following link How to get the timezone offset in GMT(Like GMT+7:00) from android device?

Dave
  • 2,829
  • 3
  • 17
  • 44
Zain SMJ
  • 1,492
  • 7
  • 19
  • 33
  • What is the desired result in the last case? A string of `5:45`?? Also what do you need the offset for? There may be an even better solution to what you’re really trying to obtain. – Ole V.V. Jun 04 '18 at 13:45
  • I’d use `OffsetDateTime.now(ZoneId.systemDefault()).getOffset()` to obtain a `ZoneOffset` object and then try to format it. To do this on not-very-new Android, you need to add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your project first. – Ole V.V. Jun 04 '18 at 13:49
  • What better solutions are you talking about pal ? – Zain SMJ Jun 05 '18 at 10:40

2 Answers2

1

Using GregorianCalender you can get the timezone please check below code

 Calendar mCalendar = new GregorianCalendar();
 TimeZone mTimeZone = mCalendar.getTimeZone();
 int mGMTOffset = mTimeZone.getRawOffset();

 double sZone = (double) (TimeUnit.MINUTES.convert(mGMTOffset,TimeUnit.MILLISECONDS));
string timeDiff = sZone / 60;
Nirav Joshi
  • 1,713
  • 15
  • 28
  • 1
    With the help of this code I got 5.75 which I converted to form string like "GMT+05:45" Thanx buddy :) – Zain SMJ Jun 06 '18 at 08:39
  • Most Welcome @ZainSMJ. – Nirav Joshi Jun 07 '18 at 08:39
  • FYI, the terribly flawed date-time classes such as [`java.util.Date`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Calendar.html), `GregorianCalendar`, and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/tutorial/datetime/TOC.html) classes built into Java 8 and later. – Basil Bourque Jul 13 '20 at 23:36
1

java.time

    ZoneOffset currentOffset = OffsetDateTime.now(ZoneId.systemDefault()).getOffset();
    System.out.println("Current offset is GMT" + currentOffset);

Running the above snippet today in Europe/Copenhagen time zone gave:

Current offset is GMT+02:00

Denmark is at +01:00 during standard time and +02:00 during summer time (DST). We are currently using summer time, so the output is as expected and does take summer time into account.

Not knowing Xamarin I am giving you Java code and trusting you to convert.

If instead I set my JVM’s time zone to Asia/Kathmandu I get the offset you asked for:

Current offset is GMT+05:45

Link: Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161