Android Kotlin Convert Timestamp Milliseconds to Seconds and Vise-Versa

masterjosh
by
masterjosh
Jan 10, 2019
Android Kotlin Convert Timestamp Milliseconds to Seconds and Vise-Versa

Hi Dev,

this pretty little snippet am about to share, will allow you to convert php timestamp seconds to Java milliseconds and Java milliseconds to seconds format...

Milliseconds to Seconds

Fun/method code named timestampToSec


fun timestampToSec(time: Long): Long {
        return if (time < 1000000000000L) {
            // if timestamp given in seconds, convert to millis
            time
        } else {
            time / 1000
        }
    }

Seconds to Milliseconds

Fun/method code named timestampToMilli


fun timestampToMilli(time: Long): Long {
        return if (time < 1000000000000L) {
            // if timestamp given in seconds, convert to millis
            time * 1000
        } else {
            time
        }
    }

are you have any problem using this code ?

i will be happy to help via comment section 

Happy Coding!




Add Comment