question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DateTimeFormatter equivalent

See original GitHub issue

It is not included in the documentation so we would like to ask if Kotlinx Datetime has java.time.format.DateTimeFormatter equivalent for formatting?

Here are the following usage we had with the Java DateTimeFormatter

val formattedDate: String
        get() = LocalDate.parse(date, DateTimeFormatter.ISO_LOCAL_DATE_TIME).format(
            DateTimeFormatter.ofPattern("MMM. dd, yyyy")
        )
  val formattedTime: String = Instant.ofEpochMilli(time).atZone(ZoneId.systemDefault())
  .toLocalDate().format(DateTimeFormatter.ofPattern("MM-dd-yyyy"))
LocalDateTime.parse(
                  lowDate,
                  DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'")
              )

Currently using coreLibraryDesugaringEnabled for backward compatibility.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
SamCosta1commented, Oct 20, 2022

My usecase was very limited (and only needing to support Android & iOS) so I ended up just going for this very simple util, posting here incase it helps someone.

In commonMain:

import kotlinx.datetime.LocalDateTime

expect fun LocalDateTime.format(format: String): String

androidMain:

import java.time.format.DateTimeFormatter

import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.toJavaLocalDateTime

actual fun LocalDateTime.format(
    format: String
): String = DateTimeFormatter.ofPattern(format).format(this.toJavaLocalDateTime())

iosMain:

import platform.Foundation.NSCalendar
import platform.Foundation.NSDateFormatter

import kotlinx.datetime.LocalDateTime

actual fun LocalDateTime.format(format: String): String {
    val dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = format
    return dateFormatter.stringFromDate(
        toNSDate(NSCalendar.currentCalendar)
            ?: throw IllegalStateException("Could not convert kotlin date to NSDate $this")
    )
}
2reactions
dkhalanskyjbcommented, May 30, 2022

Sorry but does that mean it is not yet supported?

Yes.

Regarding the LocalDateTime.parse usage though, it closely resembles ISO-8601, so you can do that:

Instant.parse(lowDate).toLocalDateTime(TimeZone.UTC)
Read more comments on GitHub >

github_iconTop Results From Across the Web

DateTimeFormatter (Java Platform SE 8 ) - Oracle Help Center
Four letters outputs the full form of localized offset, equivalent to four letters of Offset-O. The output will be the corresponding localized offset...
Read more >
Code equivalent from java 8 to java 7 of DateTimeFormatter ...
This question already has answers here:​​ DateTimeFormatter formatter = DateTimeFormatter. ofPattern("yyyyMMdd"); String fecha = LocalDate. now() ...
Read more >
Convert DateTimeFormatter withZoneUTC to java.time ...
Recipe Name: Convert DateTimeFormatter withZoneUTC to java.time equivalent ; Description: Convert DateTimeFormatter withZoneUTC to java.time equivalent ; Level:.
Read more >
DateTimeFormatter - Android Developers
LocalDate date = LocalDate.now(); DateTimeFormatter formatter = DateTimeFormatter. ... Four letters outputs the full form of localized offset, equivalent to ...
Read more >
DateTimeFormatter.ShortTime Property - UWP - Microsoft Learn
A DateTimeFormatter object equivalent to one constructed with the "shorttime" template. Applies to. Product, Versions. WinRT, Build 10240, Build 10586, Build ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found