0

I am looking for an ability to describe format templates (eg. used in java.util.Formatter) for dates (like java.text.SimpleDateFormat) and numbers (like java.text.DecimalFormat) that could be transparently used to format values both on the backend (jvm) side and on the browser (js) side.

I hoped that kotlin can provide this feature, but my research stopped at feature requests like https://youtrack.jetbrains.com/issue/KT-21644. So as I see I can not get something ready to use with current kotlin version 1.5.31.

So my question is about some posible format template standarts or may be external libraries which can help me to format number and date values in a single approach on backend and frontend side. What I might need to pay attention to get single format template engine?

2 Answers 2

1

For date formatting maybe https://github.com/Kotlin/kotlinx-datetime could help.

At the moment probably is no complete solution for your problem (could you perhaps provide more detail on what you're trying to do? Date Formatting and Decimal Formatting or a common way to format?), one thing you could do is:

  • Redefine Formatter in your common code in a non jvm way as an expected declaration.
  • Define actual implementations for js and jvm (on the jvm side you could probably use typealiases that way you practically get the effect of redefining the actual implementation for js)

Here is an example on how that's done for Android and iOS. Alternatively this blog could offer some inspiration for the typealias part: https://nrobir.medium.com/uploading-images-in-kotlin-multiplatform-ecf87e866505

Sign up to request clarification or add additional context in comments.

Comments

0

I am testing now in our commercial projects the Java.format/sprintf-like multiplatform library I wrote to have it working the same in JS, native and JVM. It supports JS Date(), multimplatform lotlinx.datetime and most native datetime formats in JVM, in addition to string/number formatting. Your welcome: https://github.com/sergeych/mp_stools.

It has a almost full set of %t* formats for date/time processing and could be added as a dependency (also MP).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.