Kotlin .offsetAt()
Published Nov 14, 2023
Contribute to Docs
The .offsetAt() method is used to find the offset from UTC(Coordinated Universal time) for a specific time zone at a specified instant in physical time.
Syntax
fun TimeZone.offsetAt(instant: Instant): UtcOffset
Instant: A moment in time.UtcOffset: An offset from UTC (Coordinated Universal Time).TimeZone: Provides a conversion betweenInstantandLocalDateTimevalues using a collection of rules.
Example
The example given below provides the offset for Asia/Kolkata time zone at a specified instant.
import kotlinx.datetime.Instantimport kotlinx.datetime.TimeZonefun main() {val instant = Instant.parse("2023-11-07T12:00:00Z")val timeZone = TimeZone.of("Asia/Kolkata")val offset = timeZone.offsetAt(instant)println("At $instant, the offset from UTC in ${timeZone.id} is $offset.")}
Note: The string
2023-11-07T12:00:00Zspecifies date and time of November 7, 2023, at 12:00:00 UTC. TheTseparates the date and time components, andZindicates that the time is in UTC.
The above example will result in the following output:
At 2023-11-07T12:00:00Z, the offset from UTC in Asia/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