我试图通过乘以毫秒来计算30天,但结果不断变为days_30值的负数,我不知道为什么.
任何建议都非常感谢!
代码链:
// check to ensure proper time has elapsed
SharedPreferences pref = getApplicationContext()
.getSharedPreferences("DataCountService", 0);
long days_30 = 1000 * 60 * 60 * 24 * 30;
long oldTime = pref.getLong("smstimestamp", 0);
long newTime = System.currentTimeMillis();
if(newTime - oldTime >= days_30){
Run Code Online (Sandbox Code Playgroud)
days_30值的结果为:-1702967296
PS
double days_30 = 1000 * 60 * 60 * 24 * 30;
double oldTime = pref.getLong("smstimestamp", 0);
double newTime = System.currentTimeMillis();
if(newTime - oldTime >= days_30){
Run Code Online (Sandbox Code Playgroud)
结果较小 - 但仍为负数.-1.702967296E9
我使用以下方法在sharedPreferences中分享了当前时间:
日期日期=新日期(System.currentTimeMillis());
SharedPreferences pref = getApplicationContext().getSharedPreferences("DataCountService", 0);
long millis = date.getTime();
SharedPreferences.Editor editor = pref.edit();
editor.putLong("smstimestamp", date.getTime());
editor.commit();
Run Code Online (Sandbox Code Playgroud)
现在(稍后在源代码中)我需要将当前时间与我在共享首选项中保存的时间进行比较,以查看是否已过了30天.
这样做的最佳方法是什么?