Kotlin .offsetIn()
Published Nov 13, 2023
Contribute to Docs
The .offsetIn() function is used to obtain the time zone offset at a specific point in time. It provides the difference between the local time and UTC for a given moment.
Syntax
fun Instant.offsetIn(timeZone: TimeZone): UtcOffset
- Instant: A moment in time.
- UtcOffset: An offset from UTC (Coordinated Universal Time).
- TimeZone: provides a conversion between
InstantandLocalDateTimevalues using a collection of rules.
Example
import kotlinx.datetime.TimeZoneimport kotlinx.datetime.Clockimport kotlinx.datetime.offsetInfun main() {val currentTime = Clock.System.now()val timeZone = TimeZone.of("Asia/Kolkata")val offset = currentTime.offsetIn(timeZone)println("Offset from UTC in Kolkata is $offset.")}
The code given above provides the offset for the current time in the Asia/Kolkata time zone.
Note: The string
Asia/Kolkataabove is a zoneId (returns the time zone identified by the provided zoneId). These IDs are usually in the form ofarea/city.
The above example will result in the following output:
Offset from UTC in Kolkata is +05:30.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Kotlin on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn Kotlin, the expressive, open-source programming language developed by JetBrains.
- Beginner Friendly.9 hours