Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
48 lines (39 sloc) 1.4 KB
package com.codingtive.androidtutorial.kotlin
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*
object DateTimeFormat {
@JvmStatic
fun main(args: Array<String>) {
// example use format date
val formattedDate = formatDate("2019-05-22", "EEE, dd MMMM yyyy")
println("Date is $formattedDate")
// example use format time
val formattedTime = formatTime("23:15:00", "HH.mm")
println("Time is $formattedTime")
}
private fun formatDate(date: String, format: String): String {
var formattedDate = ""
val sdf = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
sdf.timeZone = TimeZone.getTimeZone("UTC")
try {
val parseDate = sdf.parse(date)
formattedDate = SimpleDateFormat(format).format(parseDate)
} catch (e: ParseException) {
e.printStackTrace()
}
return formattedDate
}
private fun formatTime(time: String, format: String): String {
var formattedTime = ""
val sdf = SimpleDateFormat("HH:mm:ss", Locale.getDefault())
sdf.timeZone = TimeZone.getTimeZone("UTC")
try {
val parseDate = sdf.parse(time)
formattedTime = SimpleDateFormat(format).format(parseDate)
} catch (e: ParseException) {
e.printStackTrace()
}
return formattedTime
}
}
You can’t perform that action at this time.