我从一个 json 文件中得到一个10 位的时间戳,我刚刚发现这是 Unix 时间,以秒为单位,而不是以毫秒为单位。
所以我去了我的 DateUtils 类,将时间戳(以秒为单位)乘以 1000,以便将其转换为以毫秒为单位的时间戳。
当我尝试测试 isToday() 时,这行代码给了我一年 50000 的东西......
int otherYear = this.calendar.get(Calendar.YEAR);
这里有什么错误?
DateUtils.java
public class DateUtils{
public class DateUtils {
private Calendar calendar;
public DateUtils(long timeSeconds){
long timeMilli = timeSeconds * 1000;
this.calendar = Calendar.getInstance();
this.calendar.setTimeInMillis(timeMilli*1000);
}
private boolean isToday(){
Calendar today = Calendar.getInstance();
today.setTimeInMillis(System.currentTimeMillis());
// Todays date
int todayYear = today.get(Calendar.YEAR);
int todayMonth = today.get(Calendar.MONTH);
int todayDay = today.get(Calendar.DAY_OF_MONTH);
// Date to compare with today
int otherYear = this.calendar.get(Calendar.YEAR);
int otherMonth = this.calendar.get(Calendar.MONTH);
int otherDay = this.calendar.get(Calendar.DAY_OF_MONTH);
if (todayYear==otherYear && todayMonth==otherMonth && todayDay==otherDay){
return true;
}
return false;
}
}