0

In Kotlin, I'am trying to retrieve the SimpleDateFormat to a String variable but I receive Error. Any help please?

var someDate:SimpleDateFormat = SimpleDateFormat("2020-05-16")
var someText:String = someDate.toString()  --> Error
Ihsan
  • 3
  • 1
  • 3
  • That's not a valid date format. It should look something like this: `SimpleDateFormat("yyyy-MM-dd HH:mm:ss")` – Daniel Nugent Dec 04 '20 at 21:52
  • 1
    Be aware that Kotlin has its own date-time handling, largely aped from the industry-leading *java.time* classes bundled with Java. Never use the terrible `SimpleDateFormat`, `Date`, and `Calendar` classes in Java — now legacy as of *java.time* defined in JSR 310. – Basil Bourque Dec 04 '20 at 23:50
  • What error are you getting? Please paste error message and/or stack trace into the question. So we may help you with the error you are getting. – Ole V.V. Dec 06 '20 at 18:28
  • When I hand translate your code to Java, it works, and `someText` becomes `java.text.SimpleDateFormat@7da0adc` (in one run). Maybe not very useful, but not an error. – Ole V.V. Dec 07 '20 at 09:40

2 Answers2

1

Disclaimer: I do not need Kotlin syntax but AFAIK, Java code can be executed as it is in Kotlin or at least anyone working with Kotlin should be able to translate a Java code easily into Kotlin syntax.


The date-time API of java.util and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API. Learn more about the modern date-time API at Trail: Date Time.

Note: 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 and How to use ThreeTenABP in Android Project.

Using the modern date-time API:

import java.time.LocalDate;

public class Main {
    public static void main(String[] args) {
        LocalDate date = LocalDate.of(2020, 5, 16);
        String formatted = date.toString();
        System.out.println(formatted);
    }
}

Output:

2020-05-16

Note that LocalDate#toString already returns the string in the ISO-8601 format, which is the format you want the output string in, and therefore you do need a formatter to get the string into the required format.

Using the legacy API:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, 2020);
        // Note that in java.util date-time API, January is month, 0
        calendar.set(Calendar.MONTH, 4);
        calendar.set(Calendar.DAY_OF_MONTH, 16);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = calendar.getTime();
        String formatted = sdf.format(date);
        System.out.println(formatted);
    }
}

Output:

2020-05-16
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

Kotlin API levels 26 or greater:

val parsedDate = LocalDateTime.parse("2018-12-14T09:55:00", DateTimeFormatter.ISO_DATE_TIME)
val formattedDate = parsedDate.format(DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm"))

Below API levels 26:

val parser =  SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
val formatter = SimpleDateFormat("dd.MM.yyyy HH:mm")
val formattedDate = formatter.format(parser.parse("2018-12-14T09:55:00"))
  • 1
    Are you mixing up Android level 26 with Kotlin level 26? And in the case of Android, that limitation is gone when using new tooling with API desugaring feature. – Basil Bourque Dec 04 '20 at 23:47