31

The Code A can convert a long value to date value, just like 2018.01.10

  1. I hope to get Date + Time value , such as 2018.01.10 23:11, how can I do with Kotlin?

  2. I hope to convert current time to a long value , how can I do with Kotlin?

Thanks!

Code A

fun Long.toDateString(dateFormat: Int =  DateFormat.MEDIUM): String {
    val df = DateFormat.getDateInstance(dateFormat, Locale.getDefault())
    return df.format(this)
}
HelloCW
  • 843
  • 22
  • 125
  • 310
  • Use `SimpleDateFormat` https://developer.android.com/reference/java/text/SimpleDateFormat.html – Vladyslav Matviienko Mar 29 '18 at 08:37
  • 1
    Thanks! can you show some code with Kotlin? – HelloCW Mar 29 '18 at 08:48
  • I could if I had time, but it is quite easy to convert code from JAva – Vladyslav Matviienko Mar 29 '18 at 08:50
  • @VladyslavMatviienko I strongly recommend *against* `SimpleDateFormat`. That class was notoriously troublesome and is fortunately long outdated. Use [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/index.html). – Ole V.V. Aug 31 '23 at 09:26
  • `return Instant.ofEpochMilli(this) .atZone(ZoneId.systemDefault()) .format(DateTimeFormatter.ofLocalizedDateTime(dateFormat));` where the `dateFormat` parameter should have type `FormatStyle` with possible values SHORT, MEDIUM, LONG and FULL – Ole V.V. Aug 31 '23 at 09:37
  • @OleV.V. necroposting at it's finest. It's fine, only 5 years has passed. – Vladyslav Matviienko Sep 01 '23 at 07:19

5 Answers5

72

Try this, I use SimpleDataFormat.

fun convertLongToTime(time: Long): String {
    val date = Date(time)
    val format = SimpleDateFormat("yyyy.MM.dd HH:mm")
    return format.format(date)
}

fun currentTimeToLong(): Long {
    return System.currentTimeMillis()
}

fun convertDateToLong(date: String): Long {
    val df = SimpleDateFormat("yyyy.MM.dd HH:mm")
    return df.parse(date).time
}

And to convert java file to kotlin file with Android Studio, choosing Code->Convert java file to kotlin file.

Tung Tran
  • 2,885
  • 2
  • 17
  • 24
8

No need for anything complex:

Get current time and date as a Date object

val dateTime: Date = Calendar.getInstance().time

Convert it to a Long

val dateTimeAsLong: Long = dateTime.time

Convert that Long back to a Date

val backToDate: Date = Date(dateTimeAsLong)
David
  • 1,050
  • 1
  • 16
  • 31
2

I like to use extension functions, as so...

Convert long to Time & Date String:

fun Long.toTimeDateString(): String {
    val dateTime = java.util.Date(this)
    val format = SimpleDateFormat("HH:mm:ss dd/MM/yyyy", Locale.US)
    return format.format(dateTime)
}

Convert Time & Date String to Long:

fun String.toTimeDateLong(): Long {
    val format = SimpleDateFormat("HH:mm:ss dd/MM/yyyy", Locale.US)
    return format.parse(this)?.time ?: throw IllegalArgumentException("Invalid time string")
}

To use:

fun main() {
   val timeDateLong = "10:23:12 12/11/2022".toTimeDateLong()
   
   val timeDateStr = timeDateLong.toTimeDateString()
}
RealityExpander
  • 133
  • 1
  • 8
0

Convert long to Date String with the format:

fun Long.convertMilliSecondsToDate(): String {
            val date = Date(this)
            val format = SimpleDateFormat("yyyy.MM.dd HH:mm", Locale.ENGLISH)
            return format.format(date)
        }
0
private val simpleDateFormat by lazy { SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()) }

fun getDate(timeStamp: Long): String {
    return simpleDateFormat.format(Date(timeStamp))
}
Sohaib Ahmed
  • 1,990
  • 1
  • 5
  • 23