我在我的数据库中有这种格式的日期2016-03-11 09:20:22 如果我想与今天的日期比较,我该怎么办? 我尝试做到这一点,但我认为效率不高:
private int getDayOfYear(String dateString) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(dateString);
GregorianCalendar greg = new GregorianCalendar();
greg.setTime(date);
return greg.get(greg.DAY_OF_MONTH);
}
private int getMonth(String dateString) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(dateString);
GregorianCalendar greg = new GregorianCalendar();
greg.setTime(date);
return greg.get(greg.MONTH);
}
private int getYear(String dateString) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(dateString);
GregorianCalendar greg = new GregorianCalendar();
greg.setTime(date);
return greg.get(greg.YEAR);
}
现在我申请今天的日期和db date这个方法,还有另一种比较两个日期的方法吗?