https://kotlinlang.org logo
#kotlinx-datetime
Title
# kotlinx-datetime
k

kevin.cianfarini

01/17/2023, 4:42 PM
When delegating formatting of a
LocalDateTime
to javas datetime utilities, how would I format the full date and time? Right now the value
Copy code
LocalDateTime(
  year = 2023,
  monthNumber = 1,
  dayOfMonth = 17,
  hour = 10,
  minute = 30
)
Gets formatted to
Tuesday, January 17, 2023
with the following invocation:
Copy code
DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
            .withLocale(javaLocale)
            .format(dateTime.toJavaLocalDateTime())
I would expect this to format to something like
Tuesday, January 17, 2023 10:30 AM
.
I can’t use a static pattern because the output string needs to be locale sensitive
I think this is probably a java question actually
h

hfhbd

01/17/2023, 4:45 PM
Only a guess: use
toJavaLocalDateTime()
k

kevin.cianfarini

01/17/2023, 4:45 PM
Oh whoops I do, I also have a function for
LocalDate
and
LocalTime
.
Copy code
DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
            .withLocale(javaLocale)
            .format(dateTime.toJavaLocalDateTime())
Oh I did just see
DateTimeFormatter.ofLocalizedDateTime
. That’s probably what I need.
I needed to pass in separate date and time formatstyles.
Copy code
DateTimeFormatter.ofLocalizedDateTime(
    FormatStyle.FULL,
    FormatStyle.SHORT
).withLocale(javaLocale).format(dateTime.toJavaLocalDateTime())
which yields
Tuesday, January 17, 2023, 10:30 AM
You cannot use the
FULL
format style on
kotlinx.LocalDateTime
for the time component because local date times don’t have timezone info embedded.
10 Views