0

为了将天数添加到特定日期,我尝试过这样

calendar.setTime(currentDate);
calendar.add(Calendar.DAY_OF_YEAR, 7);
Date nextWeek = calendar.getTime();
System.out.format("next week:  %s\n", nextWeek);

但我正在使用它来获取应用安装日期

    PackageManager pm = getApplicationContext().getPackageManager();
                ApplicationInfo appInfo = pm.getApplicationInfo(
                        "com.mothistorycheck.functions", 0);
                String appFile = appInfo.sourceDir;
                long installed = new File(appFile).lastModified();
                Date date = new Date(installed * 1000L);
                Date currentDate = Calendar.getInstance().getTime();

我想从这个安装日期提前 1 年,如何获得它。

4

2 回答 2

0

尝试这个:

PackageManager pm = getApplicationContext().getPackageManager();
ApplicationInfo appInfo = pm.getApplicationInfo("com.mothistorycheck.functions", 0);
String appFile = appInfo.sourceDir;
File file = new File(appFile);
Date installed = new Date(file.lastModified());


Calendar calendar = Calendar.getInstance();
calendar.setTime(installed);
calendar.add(Calendar.YEAR,1);
Date installedPlusYear = calendar.getTime();
于 2013-10-24T13:33:22.273 回答
0

假设这里是普通的java(并且appFile是一个最新的值)

    File f = new File(System.getProperty("user.home"));
    long lastMod = f.lastModified();
    System.out.println("last mod: "+lastMod);
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(lastMod);
    System.out.println("date: "+c.getTime());
    c.add(Calendar.YEAR, 1);
    System.out.println("new date:"+c.getTime());

输出:最后一个模块:1382558398000 日期:2013 年 10 月 24 日星期四 08:26:47 CDT 2013 新日期:2014 年 10 月 24 日星期五 08:26:47 CDT

于 2013-10-24T13:27:42.913 回答