Rubit Rubit - 1 month ago 16
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
// 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);


You're using this SimpleDateFormat that resides under following package 

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