将天数添加到特定日期我试过这样
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年,如何获得它。
答案 0 :(得分: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());
输出: last mod:1382558398000 日期:星期四10月24日08:26:47 CDT 2013 新日期:Fri Oct 24 08:26:47 CDT 2014
答案 1 :(得分: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();