首页 > 代码库 > DateUtil

DateUtil

1、比较两个日期是否是同一天

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Date lastRecordTimeD = sdf.parse(lines[2]);

Calendar lastRecordTimeCal = new GregorianCalendar();

lastRecordTimeCal.setTime(lastRecordTimeD);

Calendar nowTime = Calendar.getInstance();

Calendar tempDate = (Calendar) nowTime.clone();

tempDate.set(Calendar.YEAR, lastRecordTimeCal.get(Calendar.YEAR));
tempDate.set(Calendar.MONTH, lastRecordTimeCal.get(Calendar.MONTH));
tempDate.set(Calendar.DAY_OF_MONTH,lastRecordTimeCal.get(Calendar.DAY_OF_MONTH));

//是否是同一天
boolean sameDay = false;

if (nowTime.equals(tempDate)) {
    sameDay = true;
}

 

DateUtil