Unix Timestamp & Epoch Conversion in Kotlin
Kotlin is a cross-platform, statically typed, general-purpose programming language with type inference. It is a modern programming language that makes developers happier. It provides many date time functions to handle date time functionality.
Here we will explain Kotlin date time functions to get current epoch or Unix timestamp, convert timestamp to date and convert date to epoch or Unix timestamp.
Get current epoch or Unix timestamp in KotlinYou can get the current unix timestamp using currentTimeMillis()
.
System.currentTimeMillis()
Convert epoch or Unix timestamp to date in Kotlin
You can convert unix timestamp to date as follows.
val timeStamp = Timestamp(System.currentTimeMillis())
val date = Date(timeStamp.getTime())
Convert date to epoch or unix timestamp in Kotlin
You can convert the date to unix timestamp using following.
val date = SimpleDateFormat("dd-MM-yyyy").parse("04-07-2021")
println(date.time)
More about date time in Kotlin
Also, read:
- Unix Timestamp & Epoch Conversion in PHP
- Unix Timestamp & Epoch Conversion in Java
- Unix Timestamp & Epoch Conversion in JavaScript
- Unix Timestamp & Epoch Conversion in Perl
- Unix Timestamp & Epoch Conversion in Python
- Unix Timestamp & Epoch Conversion in TypeScript
- Unix Timestamp & Epoch Conversion in MySQL
- Unix Timestamp & Epoch Conversion SQL Server
- Unix Timestamp & Epoch Conversion in C# (C-Sharp)
- Unix Timestamp & Epoch Conversion in Go
- Unix Timestamp & Epoch Conversion in Ruby
- Unix Timestamp & Epoch Conversion in VBA
- Unix Timestamp & Epoch Conversion in MATLAB
- Unix Timestamp & Epoch Conversion in Rust