Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SimpleDateFormat gives API Error

Tags:

java

android

Whenever I use my SimpleDateFormat in android it keep giving me the error call requires API level 24. Which doesn't make sense since I refer to this tutorial which was a couple of years old.

This is the code that giving out the error

SimpleDateFormat formatter = new SimpleDateFormat("EEE,dd MMM,yyyy HH:mm:ss");

I even try but it's still not working

SimpleDateFormat formatter = new SimpleDateFormat("EEE,dd MMM,yyyy HH:mm:ss",Locale.US);

Here is my method where I'm having problem with

public static long getDateInMillis(String srcDate) {

    SimpleDateFormat formatter = new SimpleDateFormat("EEE,dd MMM yyyy HH:mm:ss");

    long dateInMillis = 0;
    try {
        Date date = formatter.parse(srcDate);
        dateInMillis = date.getTime();
        return dateInMillis;
    }

    catch (java.text.ParseException e) {
        e.printStackTrace();
    }

    return 0;
}
like image 326
azriebakri Avatar asked Aug 20 '16 15:08

azriebakri


People also ask

Why is SimpleDateFormat not thread-safe?

2.2.Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally. So SimpleDateFormat instances are not thread-safe, and we should use them carefully in concurrent environments.

Is SimpleDateFormat deprecated?

Deprecated. A class for parsing and formatting dates with a given pattern, compatible with the Java 6 API.

Should I use SimpleDateFormat?

No, the SimpleDateFormat class is not thread-safe. If you want to share an instance of it between threads, you must synchronize access within each thread. A better alternative is the DateTimeFormatter class. It was introduced in Java 8.

What is the difference between DateFormat and SimpleDateFormat?

The java SimpleDateFormat allows construction of arbitrary non-localized formats. The java DateFormat allows construction of three localized formats each for dates and times, via its factory methods.

What is the use of SimpleDateFormat in Java?

SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting.


1 Answers

It's because you imported android.icu.text.SimpleDateFormat instead of java.text.SimpleDateFormat.

like image 73
EpicPandaForce Avatar answered Nov 15 '22 19:11

EpicPandaForce