Skip to content

Instantly share code, notes, and snippets.

@carolinemusyoka
Last active August 7, 2022 13:48
Show Gist options
  • Save carolinemusyoka/c30c3fb934cae954843c40ff18585f57 to your computer and use it in GitHub Desktop.
Save carolinemusyoka/c30c3fb934cae954843c40ff18585f57 to your computer and use it in GitHub Desktop.
convert date
fun String.convertDate(context: Context): String? {
return try{
// "2021-11=10T16:24:21.579537Z"
val currentLocale = ConfigurationCompat.getLocales(context.resources.configuration)[0]
val inputFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault())
inputFormat.timeZone = TimeZone.getTimeZone("GMT")
val passedDate: Date = inputFormat.parse(this) as Date
// Form example Mon, Nov *, 2021, 12:10 pm
val outputFormatDay = SimpleDateFormat("EEE, MMM d, yyyy, hh:mm aaa", currentLocale)
outputFormatDay.timeZone = TimeZone.getDefault()
val newDateString = outputFormatDay.format(passedDate)
newDateString
} catch(_: Exception){
"00:00:00"
}
}
// credits, @ronnieotieno
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment