7

I'm converting some code from using Java 8's LocalDatetime to using the version from kotlinx-datetime and I can't find any formatting methods. Specifically, I'm replacing a FormatStyle.MEDIUM. Do they not exist and I need to write the formatting?

This is for an Android app. Is there are there Android specific libraries I can use? Or can I do this with pre-Java 8 methods to maintain support for older versions of Android?

Edit (my solution based on the answer from Arvind):

fun Instant.toDateTimeString(formatStyle: FormatStyle = FormatStyle.MEDIUM): String {
    val localDatetime = toLocalDateTime(TimeZone.currentSystemDefault())
    val formatter = DateTimeFormatter.ofLocalizedDateTime(formatStyle)
    return formatter.format(localDatetime.toJavaLocalDateTime())
}
Sean
  • 2,632
  • 2
  • 27
  • 35

1 Answers1

8

As per the documentation, in a JVM, the implementation of date/time types, such as Instant, LocalDateTime, TimeZone and so on, relies on java.time API. It also natively supports ThreeTen backport project using which you can backport most of the java.time functionality to Java 6 & 7. Check How to use ThreeTenABP in Android Project to learn how to set it up.

In case you want to replace an OOTB (Out-Of-The-Box) format e.g FormatStyle.MEDIUM, you can always define a custom format using DateTimeFormatter e.g.

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.MEDIUM);

        // Custom equivalent format with a fixed Locale
        DateTimeFormatter dtfCustom = DateTimeFormatter.ofPattern("d MMM uuuu, HH:mm:ss", Locale.ROOT);

        LocalDateTime ldt = LocalDateTime.now();

        System.out.println(ldt.format(dtf));
        System.out.println(ldt.format(dtfCustom));
    }
}

Output:

6 Nov 2021, 12:18:26
6 Nov 2021, 12:18:26

ONLINE DEMO

Learn more about the modern Date-Time API* from Trail: Date Time.


* If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring. Note that Android 8.0 Oreo already provides support for java.time. Check this answer and this answer to learn how to use java.time API with JDBC.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • 1
    Thanks! I missed that part of the docs. So I guess I'll use the Java 8 API with desugaring and add an implementation with Joda time when/if we support a JS target. – Sean Nov 06 '21 at 13:37
  • I'm targeting iOS also so I can't rely on the JVM, is there any other way of formatting? – KenIchi Aug 21 '23 at 05:21
  • @KenIchi, I do not have experience in iOS but looking at [this post](https://stackoverflow.com/q/35700281/10819573), it looks like Java's way of formatting/parsing date-time is mostly the same in iOS. – Arvind Kumar Avinash Aug 21 '23 at 08:52