Get timezone android kotlin

How to get TimeZone from android mobile?

I want to get the time zone from the Android mobile when clicking a button.

12 Answers 12

Have you tried to use TimeZone.getDefault() :

Most applications will use TimeZone.getDefault() which returns a TimeZone based on the time zone where the program is running.

TimeZone GMT+09:30 Timezone id :: Australia/Darwin

Edit: corrected the case

I needed the offset that not only included day light savings time but as a numerial. Here is the code that I used in case someone is looking for an example.

I get a response of «3.5» (3:30′) which is what I would expect in Tehran , Iran in winter and «4.5» (4:30′) for summer .

I also needed it as a string so I could post it to a server so you may not need the last line.

for getting currect time zone :

It will return user selected timezone.

ZoneId from java.time and ThreeTenABP

When I ran this snippet in Australia/Sydney time zone, the output was exactly that:

If you want the summer time (DST) aware time zone name or abbreviation:

Источник

How to convert time from server to local zone with kotlin?

I get a time from server and I want to change it to Local zone How can I do that with Kotlin ?
Time coming from the server is like «2020-09-01T13:16:33.114Z»
Here’s my code:

Читайте также:  Мультимедийный центр для автомобиля 2 din с навигацией андроид

order.creationDate : Time from server

1 Answer 1

This will convert the example String to the system default time zone:

The output depends on the system default time zone, in the Kotlin Playground, it produces the following line:

which obviously means the Kotlin Playground is playing in UTC.

It’s strongly recommended to use java.time nowadays and to stop using the outdated libraries for datetime operations ( java.util.Date , java.util.Calendar along with java.text.SimpleDateFormat ).

If you do that, you can parse this example String without specifying an input format because it is formatted in an ISO standard.

You can create an offset-aware ( java.time.OffsetDateTime ) object or a zone-aware one ( java.time.ZonedDateTime ), that’s up to you. The following example(s) show(s) how to parse your String , how to adjust a zone or an offset and how to print in a different format:

For a conversion to the system zone or offset, use ZoneId.systemDefault() or ZoneOffset.systemDefault() instead of hard coded ones. Pay attention to the ZoneOffset since it does not necessarily give the correct one because only a ZoneId considers daylight saving time. For more information, see this question and its answer

For further and more accurate information about formats to be defined for parsing or formatting output, you should read the JavaDocs of DateTimeFormatter .

Источник

How to get the timezone offset in GMT(Like GMT+7:00) from android device?

I am getting the timezone of a android device using this code

But it always return me the timezone like » IST » but i want to get the timezone in GMT like this GMT+7:00.

22 Answers 22

This might give you an idea on how to implement it to your liking:

Читайте также:  Old mans journey андроид

(TimeUnit is «java.util.concurrent.TimeUnit»)

This code return me GMT offset.

It returns the time zone offset like this: +0530

If we use SimpleDateFormat below

It returns the time zone offset like this: GMT+05:30

Here is a solution to get timezone offset in GMT+05:30 this format

returns like +03:30

a one line solution is to use the Z symbol like:

where pattern could be:

full reference here:

This is how Google recommends getting TimezoneOffset.

Yet another solution to get timezone offset:

If someone is looking how to represent the GMT as a float number representing hour offset
(for example «GMT-0530» to -5.5), you can use this:

You can do like this:

Generally you cannot translate from a time zone like Asia/Kolkata to a GMT offset like +05:30 or +07:00. A time zone, as the name says, is a place on earth and comprises the historic, present and known future UTC offsets used by the people in that place (for now we can regard GMT and UTC as synonyms, strictly speaking they are not). For example, Asia/Kolkata has been at offset +05:30 since 1945. During periods between 1941 and 1945 it was at +06:30 and before that time at +05:53:20 (yes, with seconds precision). Many other time zones have summer time (daylight saving time, DST) and change their offset twice a year.

Given a point in time, we can make the translation for that particular point in time, though. I should like to provide the modern way of doing that.

java.time and ThreeTenABP

Output when running just now was:

For the default time zone set zone to ZoneId.systemDefault() .

Читайте также:  Mediatek preloader usb vcom android com10 zip

To format the offset with the text GMT use a formatter with OOOO (four uppercase letter O) in the pattern:

Источник

Оцените статью