Kotlin Android / Java String DateTime Format, API21

withoutOne picture withoutOne · Dec 14, 2018 · Viewed 34.8k times · Source

I want convert string datetime to formatted string. e.g "2018-12-14T09:55:00" to "14.12.2018 09:55" as String => Textview.text

how can I do this with kotlin or java for android ?

Answer

xingbin picture xingbin · Dec 14, 2018

Parse it to LocalDateTime then format it:

LocalDateTime localDateTime = LocalDateTime.parse("2018-12-14T09:55:00");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm");
String output = formatter.format(localDateTime);

If this does not work with api21, you can use:

SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm");
String output = formatter.format(parser.parse("2018-12-14T09:55:00"));

or import ThreeTenABP.