kirusamma
kirusamma

Reputation: 119

Kotlin - Unparseable date

I have this error when I try to parse my date

Caused by: java.text.ParseException: Unparseable date

What I want to parse

"2020-09-23T13:45:13.371Z"

Here my code

    val formatter = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault())
    val mDate = formatter.parse(dateString)

    val date = SimpleDateFormat("EEEE, MMMM d, yyyy", Locale.getDefault())
    date.timeZone = TimeZone.getTimeZone("UTC")
    return (date.format(mDate))

Can anyone point me in the right direction to parse this date string?

Best regard,

Upvotes: 3

Views: 2010

Answers (2)

nidhal belhadj salem
nidhal belhadj salem

Reputation: 35

You can use native SimpleDateFromat to parse such dates. For example:

String yourTime = "2020-09-23T13:45:13.371Z";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
    try {
        calendar.setTime(sdf.parse(yourTime));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    SimpleDateFormat output = new SimpleDateFormat("HH:mm", Locale.getDefault());
    System.out.println(output.format(calendar.getTime()));

Upvotes: 2

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79550

java.util date-time classes are outdated and error-prone and so is their formatting API, SimpleDateFormat. I suggest you should stop using them completely and switch to the modern date-time API.

If you are doing it for your 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.

Learn more about the modern date-time API at Trail: Date Time.

Using the modern date-time API:

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        System.out.println(formatDateStr("2020-09-23T13:45:13.371Z"));
    }

    static String formatDateStr(String strDate) {
        return OffsetDateTime.parse(strDate).format(DateTimeFormatter.ofPattern("EEEE, MMMM d, uuuu", Locale.ENGLISH));
    }
}

Output:

Wednesday, September 23, 2020

Using the legacy API:

Note that Z in the date-time stands for Zulu time (0-hour offset) and therefore make sure to set the time-zone to UTC.

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.TimeZone;

public class Main {
    public static void main(String[] args) throws ParseException {
        System.out.println(formatDateStr("2020-09-23T13:45:13.371Z"));
    }

    static String formatDateStr(String strDate) throws ParseException {
        DateFormat inputFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        inputFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));

        DateFormat outputFormatter = new SimpleDateFormat("EEEE, MMMM d, yyyy", Locale.ENGLISH);

        return outputFormatter.format(inputFormatter.parse(strDate));
    }
}

Output:

Wednesday, September 23, 2020

Upvotes: 3

Related Questions