Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get today's timestamp in GMT format – Kotlin

I am using below function to fetch the today’s start Timestamp in kotlin- Android.

fun getTimeStampForStartOfTheDay(): String {
    val localDate = LocalDate.now()   // your current date time
    val startOfDay: LocalDateTime = localDate.atStartOfDay() // date time at start of the date
    val timestamp = startOfDay.atZone(ZoneId.systemDefault()).toInstant()
        .epochSecond // start time to timestamp
    return timestamp.toString()
}

The TimeStamp am getting is as : 1639593000

GMT: Wednesday, 15 December 2021 18:30:00

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Your time zone: Thursday, 16 December 2021 00:00:00 GMT+05:30

Relative: 11 hours ago

But timestamp should be 1639612800

GMT: Thursday, 16 December 2021 00:00:00

Your time zone: Thursday, 16 December 2021 05:30:00 GMT+05:30

Relative: 5 hours ago

How can I achieve this ? Thanks.

>Solution :

Change ZoneId.systemDefault() to UTC TimeZone

 val timestamp = startOfDay.atZone(ZoneId.of("UTC")).toInstant().epochSecond
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading