Display the Android application.apk creation date in Application

For new readers:

public static String getAppTimeStamp(Context context) {
        String timeStamp = "";

        try {
            ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), 0);
            String appFile = appInfo.sourceDir;
            long time = new File(appFile).lastModified();

            SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        timeStamp = formatter.format(time);

        } catch (Exception e) {

        }

        return timeStamp;

    }

https://stackoverflow.com/a/2832419/1968592


Method which checks date of last modification of classes.dex, this means last time when your app's code was built:

 try{
 ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(), 0);
 ZipFile zf = new ZipFile(ai.sourceDir);
 ZipEntry ze = zf.getEntry("classes.dex");
 long time = ze.getTime();
 String s = SimpleDateFormat.getInstance().format(new java.util.Date(time));

 }catch(Exception e){
 }

I use the same strategy as Irshad Khan and Pointer Null except I prefer the MANIFEST.MF file. This one is regenerated even if a layout is modified (which is not the case for classes.dex). I also force the date to be formated in GMT to avoid confusion between terminal and server TZs (if a comparison has to be made, ex: check latest version).

It result in the following code:

  try{
     ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(), 0);
     ZipFile zf = new ZipFile(ai.sourceDir);
     ZipEntry ze = zf.getEntry("META-INF/MANIFEST.MF");
     long time = ze.getTime();
     SimpleDateFormat formatter = (SimpleDateFormat) SimpleDateFormat.getInstance();
     formatter.setTimeZone(TimeZone.getTimeZone("gmt"));
     String s = formatter.format(new java.util.Date(time));
     zf.close();
  }catch(Exception e){
  }

Tags:

Date

Android