试试这个:
private long difference ;
//This should be saved from when 2 days is to be checked
SharedPreferences myPrefs = context.getSharedPreferences("myPrefs",MODE_WORLD_READABLE);
syncdate = myPrefs.getLong("difference", System.currentTimeMillis());
String olddate = changeFormat(syncdate);
String newdate = changeFormat(System.currentTimeMillis());//This is the new date
difference = getDate(olddate, newdate);
public static String changeFormat(long date){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
Date resultdate = new Date(date);
String format = sdf.format(resultdate);
return format;
}
public static long getDate(String firstdate,String SecondDate)
{
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
String arr[] =firstdate.split("/");
String arr1[] = SecondDate.split("/");
int sty =Integer.parseInt(arr[0]);
int stm = Integer.parseInt(arr[1]);
int std = Integer.parseInt(arr[2]);
int sty1 = Integer.parseInt(arr1[0]);
int stm1 = Integer.parseInt(arr1[1]);
int std1 = Integer.parseInt(arr1[2]);
calendar1.set(sty, stm, std);
calendar2.set(sty1, stm1, std1);
long milliseconds1 = calendar1.getTimeInMillis();
long milliseconds2 = calendar2.getTimeInMillis();
long diff = milliseconds2 - milliseconds1;
long diffDays = diff / (24 * 60 * 60 * 1000);
return diffDays;
}