Kotlin Convert DateTime Format Example
Code:
Toast.makeText(
applicationContext,
getConvertedDate("2022-04-28 12:50:00", "yyyy-MM-dd HH:mm:ss", "yy-MM-dd HH:mm:aa"),
Toast.LENGTH_SHORT
).show()
Function:
@SuppressLint("SimpleDateFormat")
fun getConvertedDate(
strDate: String,
strDateInputFormat: String,
strDateOutputFormat: String
): String {
var newDate: Date? = null
val sdfInputFormat = SimpleDateFormat(strDateInputFormat)
try {
newDate = sdfInputFormat.parse(strDate)
} catch (e: ParseException) {
e.printStackTrace()
}
val sdfOutputFormat = SimpleDateFormat(strDateOutputFormat)
return sdfOutputFormat.format(newDate)
}
Comments
Post a Comment