1. Introduction
When we start working with date and time, an unpleasant truth suddenly becomes clear: a string is convenient only as long as you are just printing it to the console. But as soon as you want to compare dates, sort events, or validate user input, a string turns into quicksand. That is why we need discipline: store date/time separately as a type, and use a string only for input and output.
Parsing is converting a string into a type (LocalDate, LocalDateTime, Instant). Formatting is the reverse operation: converting a type into a string. These operations are like entering and exiting a subway: mix them up—and you are in the wrong place, plus with the feeling that someone stole your common sense.
For this lecture, it is important to hold on to the idea: we do not “store a date as a string”; we store a date as a date, and we make a string only as a human-facing “wrapper.”
2. ISO-8601 and parse()
If programmers around the world got together and agreed on a single date format, it would probably be ISO‑8601. It does not look very “human-friendly,” but it is very “machine-friendly”: 2026-01-14 is unambiguous, sorts predictably, and does not make you guess whether the middle part is the month or the day. In kotlinx.datetime, many types can parse ISO strings via parse().
Let’s agree on a practical rule right away: for interchange and storage we use ISO (what parse() understands), and for display to a person we can implement our own format (for example, dd.MM.yyyy). This separation is like the program’s “internal language” versus the “UI language.”
A small example with LocalDate:
import kotlinx.datetime.LocalDate
fun main() {
val d = LocalDate.parse("2026-01-14")
println(d) // 2026-01-14
}
Here println(d) invokes toString() (under the hood), and toString() for LocalDate typically returns the ISO form.
And here is an example with Instant:
import kotlinx.datetime.Instant
fun main() {
val t = Instant.parse("2026-01-14T12:30:00Z")
println(t) // 2026-01-14T12:30:00Z
}
Pay attention to Z: that is “UTC.” For Instant, it is important that the string defines a moment in time unambiguously (for example, via Z or an offset). Otherwise you get “local clock time with no conversion rules,” which is a different entity.
Why parse() can fail—and why that is normal
The words “may throw an exception” sound scary, but in the world of parsing it is just everyday life. If the user entered 2026-13-40, the date is impossible. If they entered hello, it does not resemble a date at all. At that point, the library has every right to say “I don’t understand”—via an exception.
In previous topics we saw a similar story with numbers: toInt() can throw NumberFormatException if the string is not a number, so for user input it is better to use toIntOrNull().
With dates/time the idea is the same; just instead of toIntOrNull() we will usually have our own “…OrNull” function built on runCatching.
3. Safe parsing: try/catch and runCatching
When you write a console program, user input is the outside world. And the outside world, as we know, is not obligated to be neat. So parsing must be safe: invalid input must not crash the program. In Kotlin there is the classic path via try/catch, and there is a more compact one via runCatching.
Variant using try/catch
In this approach we explicitly catch the exception and return null if parsing fails:
import kotlinx.datetime.LocalDate
fun parseIsoDateOrNull(input: String): LocalDate? {
return try {
LocalDate.parse(input.trim())
} catch (e: Exception) {
null
}
}
This code reads fine, but it quickly grows if you end up with many such parsers.
Variant using runCatching
runCatching lets you express “try—if it doesn’t work, don’t crash” compactly:
import kotlinx.datetime.LocalDate
fun parseIsoDateOrNull(input: String): LocalDate? =
runCatching { LocalDate.parse(input.trim()) }.getOrNull()
Here getOrNull() returns the result on success or null on failure. This is perfect for the pattern “read → prepare → parse → validate.”
Historically, there are even nuances around runCatching contracts and behavior in tricky cases, which you can see in Kotlin changelogs. But at our level the simple point matters: runCatching helps avoid turning code into a forest of try/catch.
Why we return T? instead of an error string
It is tempting to return an error text like "Invalid date format". But that quickly leads to confusion: the function supposedly parses, but in reality it returns either a date or a text. The T? contract is simpler: either you got the type, or null. And the user-facing message is the responsibility of the input/output layer (CLI), which decides what to print.
4. toString() as the default format
When you print an object with println, Kotlin automatically calls toString(). For types from kotlinx.datetime, toString() typically returns an ISO representation. This is convenient for two reasons: it is unambiguous, and you can parse it back again via parse().
That means you get a natural “data cycle” in the program:
flowchart LR
A["Date/time type"] -->|"toString()"| B["String (usually ISO)"]
B -->|"parse()"| A
In this cycle it is especially important that a type stays a type, and a string stays a string, and each tool does its job.
Example:
import kotlinx.datetime.LocalDate
fun main() {
val original = LocalDate.parse("2026-01-14")
val text = original.toString()
val parsedBack = LocalDate.parse(text)
println(text) // 2026-01-14
println(parsedBack) // 2026-01-14
}
At this point a common thought appears: “Great, then we will always show ISO to the user.” And… sometimes that is fine. But often it is more familiar for the user to see 14.01.2026, and that is where custom formatting begins.
5. Custom format and input
Formatting “for humans” is not about fancy math; it is about careful string assembly. In the basic course we will not use complex formatters or localization; we will write a clear formatting function manually, using padStart, interpolation, and date fields.
Format dd.MM.yyyy for LocalDate
import kotlinx.datetime.LocalDate
fun formatRu(date: LocalDate): String {
val dd = date.dayOfMonth.toString().padStart(2, '0')
val mm = date.monthNumber.toString().padStart(2, '0')
return "$dd.$mm.${date.year}"
}
fun main() {
val d = LocalDate.parse("2026-01-04")
println(formatRu(d)) // 04.01.2026
}
What matters here is that we establish a centralized rule. If tomorrow you decide you want the format dd/MM/yyyy, you change one function, not 25 println calls across the project.
Format dd.MM.yyyy HH:mm for LocalDateTime
LocalDateTime contains both date and time. We can extend the formatting:
import kotlinx.datetime.LocalDateTime
fun formatRu(dt: LocalDateTime): String {
val dd = dt.dayOfMonth.toString().padStart(2, '0')
val mm = dt.monthNumber.toString().padStart(2, '0')
val hh = dt.hour.toString().padStart(2, '0')
val min = dt.minute.toString().padStart(2, '0')
return "$dd.$mm.${dt.year} $hh:$min"
}
fun main() {
val dt = LocalDateTime.parse("2026-01-14T09:05:00")
println(formatRu(dt)) // 14.01.2026 09:05
}
We are not trying to “defeat every format in the world.” We implement one specific format that our application needs, and that is absolutely normal engineering practice.
User input: safely parse dd.MM.yyyy
Now for the unpleasant part: LocalDate.parse() is not required to understand the format 14.01.2026. So for a user-facing format we write a separate parser.
It is easy to fall into the trap of “I’ll just do it with split in five seconds.” And yes, you will. But then it turns out that 32.01.2026 also “parsed,” and that is not a date—it is calendar fan fiction. So the right approach is: parse the numbers, then create LocalDate via the constructor and catch an exception if the date is impossible.
import kotlinx.datetime.LocalDate
fun parseRuDateOrNull(input: String): LocalDate? {
val parts = input.trim().split(".")
if (parts.size != 3) return null
val day = parts[0].toIntOrNull() ?: return null
val month = parts[1].toIntOrNull() ?: return null
val year = parts[2].toIntOrNull() ?: return null
return runCatching { LocalDate(year, month, day) }.getOrNull()
}
Note: toIntOrNull() is the familiar “safe number parsing” that prevents NumberFormatException. In the exception documentation, this pattern is shown as the recommended alternative to unsafe toInt().
“Smart” parser: accept ISO or dd.MM.yyyy
In real programs you often want to be nice to the user and accept multiple format variants. But that kindness must be controlled; otherwise you will start “guessing” the input and accidentally interpreting one thing as another.
A compromise: first try ISO (as the strictest and most unambiguous), then try the Russian-style format.
import kotlinx.datetime.LocalDate
fun parseDateFlexibleOrNull(input: String): LocalDate? {
val s = input.trim()
return runCatching { LocalDate.parse(s) }.getOrNull()
?: parseRuDateOrNull(s)
}
This code is good because it does not smear logic across the whole project. Ideally, functions like this should live in your “input/parsing layer”: small, single-purpose, with a clear T? contract.
6. Integrating into the app: adding an operation date
Let’s imagine our practical console project is a simple expense tracker. Previously, an expense could consist of an amount and a category. Now we will add the operation date as a LocalDate. Our goal here is not to “build a super app,” but to show how parsing/formatting stops being chaos when it is extracted into functions.
Data model Expense with a date
import kotlinx.datetime.LocalDate
data class Expense(
val amount: Int,
val category: String,
val date: LocalDate,
)
Yes, it is only three fields, and yes, it already makes the program much clearer: the date is not a string; it is a LocalDate.
Entering an expense: read the date as a string and parse it
import kotlinx.datetime.LocalDate
fun readExpenseDate(): LocalDate {
while (true) {
print("Date (YYYY-MM-DD or dd.MM.yyyy): ")
val input = readln()
val date = parseDateFlexibleOrNull(input)
if (date != null) return date
println("Could not parse the date. Example: 2026-01-14 or 14.01.2026")
}
}
Here the loop does what it should: it does not “beg for perfect input,” it calmly asks again. And most importantly, parsing is not scattered throughout the code—it lives in parseDateFlexibleOrNull.
Nice printing of the expense list: format the date separately
fun printExpense(e: Expense) {
val dateText = formatRu(e.date)
println("${dateText} | ${e.category} | ${e.amount}")
// 14.01.2026 | Food | 1200
}
As a result, the output becomes “human-friendly,” while the data inside remains “machine-friendly.”
7. Error messages: keeping the reason without overdoing it
Sometimes you really want to tell the user “why” parsing failed: “month must be from 1 to 12.” For that, during debugging you can pull the exception out of runCatching, but you should not do this everywhere all the time: you will quickly start printing scary texts like IllegalArgumentException: Invalid date..., which are understandable to the library but not to a person.
Exception documentation shows well that exceptions come in different kinds and that their text is first and foremost a developer tool, not a user interface. So usually you do this: for the user—short and clear; and the detailed reason—into a log (if you have one) or into debug output.
Mini variant “extract the reason” (for example, for debugging):
import kotlinx.datetime.LocalDate
fun parseIsoDateDebug(input: String): LocalDate? {
val result = runCatching { LocalDate.parse(input.trim()) }
result.exceptionOrNull()?.let { e ->
println("DEBUG: parse error = ${e.message}")
}
return result.getOrNull()
}
This approach is useful, but it is important not to abuse it; otherwise you will turn a console program into a stream of “JVM suffering.”
8. Common mistakes
Mistake No. 1: parse a date “however it works” via split, but do not validate it on the calendar level.
Beginners often split the string into parts, convert them into numbers, and assume that is it. The problem is that you can accept 99.99.2026 as a “valid date” at the Int level. The correct approach is: after parsing the numbers, still create LocalDate(...) and catch the error via runCatching, so the calendar can give its honest “no.”
Mistake No. 2: call parse() directly on user input without error handling.
LocalDate.parse(...) and Instant.parse(...) are designed for a correct string. External input almost never guarantees correctness, so your code starts failing “at the worst possible moment” and ruins the UX. Instead, wrap parsing into runCatching { ... }.getOrNull() and return null, and then calmly ask the user again.
Mistake No. 3: mix “how to store” and “how to display.”
If you start storing the date as an already formatted string 14.01.2026, you make life harder for yourself: sorting, comparison, filters—all of it turns back into string work. It is much more robust to store LocalDate, and do formatting only right before output, with a separate function like formatRu(date).
Mistake No. 4: scatter formatting throughout the code instead of using a single function.
When the date format is smeared across println calls, in a week you will have three different date styles in one application: somewhere 2026-01-14, somewhere 14.1.2026, somewhere even without leading zeros. Fixing it turns into archaeology. A centralized formatting function solves this immediately and enforces discipline in the project.
Mistake No. 5: try to make a “universal parser for everything” too early.
Sometimes you want to support ten formats, spaces, slashes, commas, and the lunar calendar. In practice, that turns input into a guessing game and the code into a set of brittle heuristics. At a basic level it is better to keep one interchange format (ISO via parse()) and one user format (for example, dd.MM.yyyy), and if you support both, do it strictly and predictably, like in parseDateFlexibleOrNull.
GO TO FULL VERSION