Rubit Rubit - 1 year ago 206
Android Question

Format Time Requires API error

I try to format the time in my model class. It requires the first line or the error occurs. Can anyone help?

@RequiresApi(api = Build.VERSION_CODES.N)
public String getFormattedTime() {
SimpleDateFormat formatter = new SimpleDateFormat("h a");
// Visit this link for demanded Date&Time formats
// https://developer.android.com/reference/java/text/SimpleDateFormat.html
formatter.setTimeZone(TimeZone.getTimeZone(getTimeZone()));
// Here requires a method to set the TimeZone in Main Activity (info gotten from online
// resources database)
Date dateTime = new Date(getTime() * 1000);
return formatter.format(dateTime);
}

Answer Source

You're using this SimpleDateFormat that resides under following package

android.icu.text.SimpleDateFormat 

which requires API level of 24 or higher. Try using this SimpleDateFormat instead which is under

java.text.DateFormat
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download