This method returns the date of the install in String format like 12/25/2016 10:38:02
:
private String getInstallDate() {
// get app installation date
PackageManager packageManager = getActivity().getPackageManager();
long installTimeInMilliseconds; // install time is conveniently provided in milliseconds
Date installDate = null;
String installDateString = null;
try {
PackageInfo packageInfo = packageManager.getPackageInfo(getActivity().getPackageName(), 0);
installTimeInMilliseconds = packageInfo.firstInstallTime;
installDateString = MiscUtilities.getDate(installTimeInMilliseconds, "MM/dd/yyyy hh:mm:ss");
}
catch (PackageManager.NameNotFoundException e) {
// an error occurred, so display the Unix epoch
installDate = new Date(0);
installDateString = installDate.toString();
}
return installDateString;
}
MiscUtilities
/**
* Return date in specified format.
*
* @param milliSeconds Date in milliseconds
* @param dateFormat Date format
* @return String representing date in specified format
* <p>
* Date myDate = MiscUtilities.getDate(82233213123L, "dd/MM/yyyy hh:mm:ss.SSS");
*/
public static String getDate(long milliSeconds, String dateFormat) {
// Create a DateFormatter object for displaying date in specified format.
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
// Create a calendar object that will convert the date and time value in milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}