I'm not familiar with any API's for Android < 21, but I see one very easy way.
This involves you saving the time.
First, you need to get the time:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm dd/MM/yyyy");
String currentDateandTime = sdf.format(new Date());,
Then you can save the String.
That I leave up to you. You can use Shared Prefs, SQLite, internal storage or external storage.
As for parsing the date:
SimpleDateFormat format = new SimpleDateFormat("HH:mm dd/MM/yyyy");
try {
Date date = format.parse(dtStart);
System.out.println(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
And check this answer for checking if an app has been closed.
On API < 21 you have to piece together a lot of functionality to get the functionality you are asking for. You also have tto check when an app is opened and when it then is closed. When the app then is closed, you save the date.
Now you can on Android < 21 check when an app was last used.
I recommend you use this on Android < 21 and use the UsageStatsManager on Api >= 21. The way I propose requires the usage of a Service which can use a lot of power. The API levels where you need this way and not the UsageStatsManager slowly decreasing in numbers. Eventually you will be able to use the UsageStatsManager exclusively.