← Nesim Apps

java.time to Kotlin Multiplatform: What I Wrote by Hand

By Nesim Razon, independent mobile developer · 2026-11-30

#iOS#Android#Dev notes

My horoscope and moon app was written for Android with java.time, org.json and a Turkish Locale. Shared Kotlin code has none of those. This is what replaced each piece, which parts I wrote myself and how I checked that users see exactly the same text as before.

Three Android habits that do not travel

Code in the common source set of a Kotlin Multiplatform project cannot import java packages. The compiler says so quickly, but the list of places is longer than expected. Dates are the obvious one. Less obvious are the Turkish month and day names that came from Locale, the String.format calls that padded numbers, and the JSON files that were read with org.json, a class set that ships with Android and not with iOS.

I replaced java.time with kotlinx-datetime for dates and kotlin.time.Instant for moments, and org.json with kotlinx-serialization. For everything else I wrote a small file of date helpers. That file is under a hundred lines and it is the only place where the app knows how Turkish dates look.

Names of months and days: just write them down

The old code asked the system for the Turkish name of a month. Common code has no such formatter that I wanted to depend on, so the twelve month names and seven day names are now two constant lists. Extension properties on LocalDate give the name for a date: one for the month, one for the weekday.

Writing nineteen words by hand sounds like a step backwards. It has two advantages. The output can no longer change because a phone has different locale data, and the iPhone and Android versions cannot disagree. The app is Turkish only, so there is nothing to translate. If it had several languages I would put these lists next to the other translated strings.

The same thinking applies to upper casing. In Turkish the upper case of i is İ and the upper case of the dotless ı is I. A tiny function handles those two letters and leaves every other character to the standard rule.

The ISO week key

The app downloads a themed content pack for each week and names it by a key such as 2026-W46. The old code used the week based year and week of year fields from java.time. I rebuilt it from the rule behind the ISO 8601 definition: a week belongs to the year that contains its Thursday.

So the function first finds the Thursday of the week by adding four minus the weekday number, where Monday is one. The year of that Thursday is the year of the key. The week number is the day of year of the Thursday, minus one, divided by seven, plus one. That is the whole algorithm, three lines of arithmetic.

The trap this avoids is the turn of the year. The first of January 2027 is a Friday. Its Thursday is the last day of 2026, so the key is 2026-W53, not 2027-W01. Code that takes the calendar year of the date itself gets this wrong once every few years, which is exactly the kind of bug that nobody tests in November.

A small year and month type

The sky screen shows a month calendar of moon phases and lets you step to the next or previous month. The old code used YearMonth. In the shared module I wrote a data class with a year, a month and three things the screen actually needs: the length of the month, the date of a given day and a way to add months.

Adding months converts the pair into one running month number, adds the offset and splits it again, with floor division so that negative steps behave. Month length is a short when expression plus the usual leap year rule. A birthday picker needs February to allow the twenty ninth even without a year, so there is a second function for the maximum day of a month.

None of this is a claim about what the libraries can or cannot do. I wanted a type with exactly these operations and a text form of year, dash, month, because that string is also used as a key.

From org.json to a handful of extensions

The text bank behind the daily readings is a set of JSON files. The old code walked them with getJSONObject, getJSONArray and optString. Rewriting every call site to a new style would have meant touching a lot of lines that had no bugs.

Instead I added a few extension functions on JsonObject with short names: obj, arr and str for required values, and optObj, optArr and optStr for optional ones. The required ones throw when a key is missing, as before. The optional string returns an empty string when the key is absent or explicitly null. The call sites then read almost like the old ones, which made the change easy to review.

Rounding and number padding

Two more small things were hiding in the old code. One was String.format with two digits, now a function called pad2. The other was rounding. The old code rounded halves away from zero when it turned a value into a whole number, so I wrote that rule explicitly instead of picking whichever rounding function looked right.

Differences like these never crash. They change a number by one on a few days of the year, and a returning user notices before you do.

Proving that nothing changed

Before touching the code I made the old Android version write down what it produced. For every day from November 2026 to the end of 2027 and for all twelve signs, it recorded a short hash of every field of the daily reading. It also saved a few complete texts, and the times of moon sign changes, new and full moons and planetary retrograde changes.

Those files now sit next to the tests. The shared code must produce the same lines, and when it does not, the failure names the file and the line number. The test fixes the time zone to Istanbul, so it gives the same result on any machine. When I change the text bank on purpose, an environment variable rewrites the expected files, after I have looked at the difference.

This is the part I would repeat in any rewrite. Reading the new code carefully is useful. Comparing its output with the old code on more than four hundred days is evidence.

Frequently asked questions

Can common Kotlin code use java.time?

No. Code in the common source set cannot import java packages. Use kotlinx-datetime and kotlin.time, or write the few helpers you need.

How do I compute an ISO week number by hand?

Take the Thursday of the week. Its year is the week year, and the week number is (day of year of that Thursday minus 1) divided by 7, plus 1.

What is a golden test?

A test that compares current output with output saved earlier from a version you trust. It is very useful during a rewrite.

Do I have to replace org.json?

Only in shared code. org.json can stay in Android only modules, but common code needs a multiplatform parser such as kotlinx-serialization.

More from the blog