Jérémy CROS
07/18/2024, 9:17 AMval formattedArrival = arrival
.toLocalDateTime(TimeZone.currentSystemDefault())
.format(LocalDateTime.Format { byUnicodePattern("HH:mm") })
We're fine with hardcoded "HH:mm" for now.
But there's a warning which I don't understand : `Using format strings is discouraged. If the format string is a constant, the corresponding builder-style Kotlin code can be obtained by calling DateTimeFormat.formatAsKotlinBuilderDsl
on the resulting format.`
They only way I've been able to use the mentionned DSL is that:
val format = LocalDateTime.Format { byUnicodePattern("hh:mm") }
val test = DateTimeFormat.formatAsKotlinBuilderDsl(format)
But 1) the warning is still there 2) that doesn't really help me understand what the warning is supposed to mean.
Any help? 🙂Dmitry Khalanskiy [JB]
07/18/2024, 9:29 AMDateTimeFormat.formatAsKotlinBuilderDsl(format)
and copy-paste the result into your code.
import kotlinx.datetime.*
import kotlinx.datetime.format.*
fun main() {
val format = LocalDateTime.Format { byUnicodePattern("HH:mm") }
println(DateTimeFormat.formatAsKotlinBuilderDsl(format))
}
prints
hour()
char(':')
minute()
so your code becomes
LocalDateTime.Format { hour(); char(':'); minute() }
Jérémy CROS
07/18/2024, 9:35 AMDmitry Khalanskiy [JB]
07/18/2024, 9:41 AMJérémy CROS
07/18/2024, 9:55 AMOliver.O
07/18/2024, 2:03 PMUsing format strings like "HH:mm" is discouraged. Please see <DOC REFERENCE>.
With <DOC REFERENCE>
stating:
Consider using the DSL instead of a format string like "HH:mm".
The following example prints the corresponding DSL invocations for a given format string constant:
import kotlinx.datetime.*
import kotlinx.datetime.format.*
fun main() {
val format = LocalDateTime.Format { byUnicodePattern("HH:mm") }
println(DateTimeFormat.formatAsKotlinBuilderDsl(format))
}
The output will be:
hour()
char(':')
minute()
You can use this to replace byUnicodePattern("HH:mm")
.