Skip to content

adrcotfas/kotlinx-datetime-names

Repository files navigation

kotlinx-datetime-names

Maven Central License

A Kotlin Multiplatform library that provides localized display names for kotlinx-datetime types.

What is it?

This library extends kotlinx-datetime by adding extension functions to get localized display names for DayOfWeek and Month enums, as well as format LocalDateTime, LocalDate, and LocalTime values. It supports multiple text styles (FULL, SHORT, NARROW) and format styles, using platform-specific localization APIs to provide accurate, locale-aware formatting.

Installation

dependencies {
    implementation("io.github.adrcotfas:kotlinx-datetime-names:0.1.1")
}

Usage

Basic Usage

import io.github.adrcotfas.datetime.names.*
import kotlinx.datetime.DayOfWeek
import kotlinx.datetime.Month

// Get localized day names
val monday = DayOfWeek.MONDAY
println(monday.getDisplayName()) // "Monday" (or localized equivalent)

// Get localized month names
val january = Month.JANUARY
println(january.getDisplayName()) // "January" (or localized equivalent)

Advanced Usage with Locale and Text Style

import io.github.adrcotfas.datetime.names.*
import kotlinx.datetime.DayOfWeek
import kotlinx.datetime.Month

// Specify locale and text style
val locale = java.util.Locale.GERMAN // or platform-specific locale
val day = DayOfWeek.MONDAY

// Full name
println(day.getDisplayName(TextStyle.FULL, locale)) // "Montag"

// Short name
println(day.getDisplayName(TextStyle.SHORT, locale)) // "Mo"

// Narrow name
println(day.getDisplayName(TextStyle.NARROW, locale)) // "M"

Formatting Date and Time

The library also provides extension functions to format LocalDateTime, LocalDate, and LocalTime with locale-aware formatting:

import io.github.adrcotfas.datetime.names.*
import kotlinx.datetime.*

val dateTime = LocalDateTime(2024, 12, 1, 15, 30, 0)
val date = LocalDate(2024, 12, 1)
val time = LocalTime(15, 30, 0)

// Format LocalDateTime with separate date and time styles
dateTime.format(
    dateStyle = FormatStyle.FULL,
    timeStyle = FormatStyle.MEDIUM
) // "Sunday, December 1, 2024, 3:30:00 PM" (default locale)

// Format LocalDate
date.format(FormatStyle.SHORT) // "12/1/24" (default locale)
date.format(FormatStyle.FULL)  // "Sunday, December 1, 2024" (default locale)

// Format LocalTime
time.format(FormatStyle.SHORT)  // "3:30 PM" (default locale)
time.format(FormatStyle.MEDIUM) // "3:30:00 PM" (default locale)

// Format with different locales - same date, different results
val locale = java.util.Locale.GERMAN // or platform-specific locale

date.format(FormatStyle.FULL)
// Default locale: "Sunday, December 1, 2024"
// German locale: "Sonntag, 1. Dezember 2024"

date.format(FormatStyle.SHORT, locale)
// "01.12.24" (German format)

time.format(FormatStyle.MEDIUM, locale)
// "15:30:00" (24-hour format in German)

dateTime.format(
    dateStyle = FormatStyle.LONG,
    timeStyle = FormatStyle.SHORT,
    locale = locale
)
// "1. Dezember 2024, 15:30"

Available Styles

TextStyle (for day/month names):

  • TextStyle.FULL - Full display name (e.g., "Monday", "January")
  • TextStyle.FULL_STANDALONE - Full standalone name
  • TextStyle.SHORT - Short display name (e.g., "Mon", "Jan")
  • TextStyle.SHORT_STANDALONE - Short standalone name
  • TextStyle.NARROW - Narrow name (e.g., "M", "J")
  • TextStyle.NARROW_STANDALONE - Narrow standalone name

FormatStyle (for date/time formatting):

  • FormatStyle.SHORT - Shortest format (e.g., "12/1/24", "3:30 PM")
  • FormatStyle.MEDIUM - Medium length format (e.g., "Dec 1, 2024", "3:30:00 PM")
  • FormatStyle.LONG - Long format with more detail
  • FormatStyle.FULL - Longest format (e.g., "Sunday, December 1, 2024")

Demo

The library includes a demo app that showcases localized names across different locales and text styles.

Android Demo iOS Demo Android Demo iOS Demo

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

About

A Kotlin Multiplatform library that provides localized display names for kotlinx-datetime types.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published