hello everyone, does anyone knows how could i pars...
# android
l
hello everyone, does anyone knows how could i parse this date string: "2019-08-16 221646.919688+00" to a dd/mm/yyyy format?
😶 5
l
I know i should a SimpleDateFormat(pattern).format(dateString)
But what should i use in the pattern parameter?
I'm trying this pattern:
Copy code
yyyy-mm-dd H:m:s.z
t
lowercase ‘m’ is minute in hour. The date part would be:
yyyy-MM-dd
The time part would be:
HH:mm:ss.SSSSSS
The time zone offset at the end is:
X
(assuming it is an iso8601 timezone offset 🤷) So all together:
yyyy-MM-dd HH:mm:ss.SSSSSSX
Copy code
@Test
fun `convert sample date`() {
    val time = "2019-08-16 22:16:46.919688+00"
    val pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSX")

    val parsed = ZonedDateTime.parse(time, pattern)

    println(parsed)
}
h
You can take reference from this.
Copy code
const val PATTERN_DATE_RECEIVED = "yyyy-MM-dd"
const val PATTERN_DATE_FORMATTED = "MM/dd/yyyy"
private fun getFormattedDate(dateTime: String?): String {
    val input = SimpleDateFormat(PATTERN_DATE_RECEIVED)
    val output = SimpleDateFormat(PATTERN_DATE_FORMATTED)
    try {
        return output.format(input.parse(dateTime?.substring(0, 10) ?: "") ?: "")
    } catch (e: ParseException) {
        e.printStackTrace()
    }
    return ""
}