Android Format Java Time/LocalDateTime to Localize Date String

Oct 4, 2019

Solution 1: DateUtil

val now = LocalDateTime.now()

Ommit year if current year

// 4 Octoberval text = DateUtils.formatDateTime(context, now.toMillis()!!, 0)

Show year if not current year

// 4 October 2018val now = LocalDateTime.now().minusYears(1)val text = DateUtils.formatDateTime(context, now.toMillis()!!, 0)

Use abbreviation

// 4 Octval text = DateUtils.formatDateTime(context, now.toMillis()!!, DateUtils.FORMAT_ABBREV_ALL)

Use abbreviation + Force show year

// 4 Oct 2019val text = DateUtils.formatDateTime(context, now.toMillis()!!, DateUtils.FORMAT_ABBREV_ALL or DateUtils.FORMAT_SHOW_YEAR)

NOTE: Refer LocalDateTime.toMillis

Solution 2: DateTimeFormatter

val formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)// 4 Oct 2019val text = now.format(formatter)
val formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG)// 4 October 2019val text = now.format(formatter)
val formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)// 04/10/2019val text = now.format(formatter)

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.