Skip to content

DateTimeFormatter equivalent #211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ArcherEmiya05 opened this issue May 26, 2022 · 7 comments
Closed

DateTimeFormatter equivalent #211

ArcherEmiya05 opened this issue May 26, 2022 · 7 comments
Labels
formatters Related to parsing and formatting
Milestone

Comments

@ArcherEmiya05
Copy link

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.

@JakeWharton
Copy link

Have you seen the issues labeled with 'formatter'?

@ArcherEmiya05
Copy link
Author

Have you seen the issues labeled with 'formatter'?

You mean this?

Flexible locale-neutral parsing and formatting facilities are needed to support various date/time interchange formats that are used in practice (in particular, various RFCs).

Sorry but does that mean it is not yet supported?

@dkhalanskyjb dkhalanskyjb added the formatters Related to parsing and formatting label May 27, 2022
@dkhalanskyjb
Copy link
Collaborator

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)

@ArcherEmiya05
Copy link
Author

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)

Thanks a lot, is there any alternative formatter we can use specially when working with KMM?

@jlnstrk
Copy link

jlnstrk commented Jun 9, 2022

@ArcherEmiya05 Because it is on the timeline, I ended up just writing a simple common shim for formatting/parsing which delegates to klock under the hood, since that library has a working multiplatform implementation. I'll be switching over as soon as this one also does. For pet projects at least, maybe this will also work for you.

@SamCosta1
Copy link

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")
    )
}

@CherryLover
Copy link

On the basis of @SamCosta1 , I changed it again, and corrected the possible parameter errors.

in common Main

expect fun Instant.formatDate(pattern: String, defValue: String = ""): String

expect fun String.parseDate(pattern: String, defValue: Long = 0L): Long

fun Long.formatDate(pattern: String, defValue: String = ""): String {
  return Instant.fromEpochMilliseconds(this).formatDate(pattern, defValue)
}

in iosMain

actual fun Instant.formatDate(pattern: String, defValue: String): String {
  return try {
    val dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = pattern
    dateFormatter.stringFromDate(
      toNSDate()
    )
  } catch (e: Exception) {
    defValue
  }

}

actual fun String.parseDate(pattern: String, defValue: Long): Long {
  return try {
    val dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = pattern
    val result = dateFormatter.dateFromString(this)?.timeIntervalSince1970?.toLong()
    if (result != null) {
      result * 1000
    } else {
      defValue
    }
  } catch (e: Exception) {
    defValue
  }
}

in JVM and Android:

actual fun Instant.formatDate(pattern: String, defValue: String): String {
  return try {
    SimpleDateFormat(pattern).format(Date(this.toEpochMilliseconds()))
  } catch (e: Exception) {
    defValue
  }
}

actual fun String.parseDate(pattern: String, defValue: Long): Long {
  return try {
    SimpleDateFormat(pattern).parse(this).time
  } catch (e: Exception) {
    defValue
  }
}

dkhalanskyjb added a commit that referenced this issue Feb 20, 2024
Fixes #39
Fixes #58
Fixes #90
Fixes #128
Fixes #133
Fixes #139
Fixes #211
Fixes #240
Fixes #83
Fixes #276
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatters Related to parsing and formatting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants
@JakeWharton @jlnstrk @SamCosta1 @CherryLover @ArcherEmiya05 @dkhalanskyjb and others