背景:
判断一下某个当前日期是不是结束日期。
package date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTest {
public static void main(String[] args) throws ParseException {
Date nowDay = new Date();
String dateStr = "2019-02-01 23:52:40";
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
Date endDate = sdf.parse(dateStr);
String str = sdf.format(new Date());
System.out.println("nowdate:"+str);
//判断结束时间是否是今天
if(nowDay.getYear() == endDate.getYear() && nowDay.getMonth() == endDate.getMonth() && nowDay.getDay() == endDate.getDay()){
System.out.println("is endDate: true");
}
}
}
输出:
nowdate:2019-02-15 10:30:24
is endDate: true
一开始单纯的以为这样直接判断年月日数值相等就是了,结果发现getYear()之类的方法返回的数值已经不是正常的年份。
public class DateTest {
public static void main(String[] args) throws ParseException {
Date nowDay = new Date();
String dateStr = "2019-02-01 23:52:40";
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
Date endDate = sdf.parse(dateStr);
String str = sdf.format(new Date());
System.out.println("nowdate:"+str);
System.out.println("nowdate year:"+nowDay.getYear()+" monther:"+nowDay.getMonth()+" day:"+nowDay.getDay());
System.out.println("enddate year:"+endDate.getYear()+" monther:"+endDate.getMonth()+" day:"+endDate.getDay());
//判断结束时间是否是今天
if(nowDay.getYear() == endDate.getYear() && nowDay.getMonth() == endDate.getMonth() && nowDay.getDay() == endDate.getDay()){
System.out.println("is endDate: true");
}
}
}
输出:
nowdate:2019-02-15 10:34:21
nowdate year:119 monther:1 day:5
enddate year:119 monther:1 day:5
is endDate: true
2019-02-15 与 2019-02-01返回的数值相等,为什么呢?
查看源码:
getYear():
@Deprecated
public int getYear() {
return normalize().getYear() - 1900;
}
getMonth()
@Deprecated
public int getMonth() {
return normalize().getMonth() - 1; // adjust 1-based to 0-based
}
getDay():
@Deprecated
public int getDay() {
return normalize().getDayOfWeek() - BaseCalendar.SUNDAY;
}
原来getDay()发放是以周为一个循环,2-1 和 2-15 都是星期五。故判断是相等。
解决办法:
使用calendar判断:
package date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateTest {
public static void main(String[] args) throws ParseException {
Date nowDay = new Date();
String dateStr = "2019-02-01 23:52:40";
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
Date endDate = sdf.parse(dateStr);
String str = sdf.format(new Date());
System.out.println("nowdate:"+str);
System.out.println("nowdate year:"+nowDay.getYear()+" monther:"+nowDay.getMonth()+" day:"+nowDay.getDay());
System.out.println("enddate year:"+endDate.getYear()+" monther:"+endDate.getMonth()+" day:"+endDate.getDay());
//判断结束时间是否是今天
// if(nowDay.getYear() == endDate.getYear() && nowDay.getMonth() == endDate.getMonth() && nowDay.getDay() == endDate.getDay()){
// System.out.println("is endDate: true");
// }
if(isSameDate(nowDay,endDate)){
System.out.println("is endDate: true");
}else{
System.out.println("is endDate: false");
}
}
private static boolean isSameDate(Date date1, Date date2) {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
boolean isSameYear = cal1.get(Calendar.YEAR) == cal2
.get(Calendar.YEAR);
System.out.println("nowdate year:"+cal1.get(Calendar.YEAR)+" enddate year:"+cal2.get(Calendar.YEAR));
boolean isSameMonth = isSameYear
&& cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
System.out.println("nowdate month:"+cal1.get(Calendar.MONTH)+" enddate month:"+cal2.get(Calendar.MONTH));
boolean isSameDate = isSameMonth
&& cal1.get(Calendar.DAY_OF_MONTH) == cal2
.get(Calendar.DAY_OF_MONTH);
System.out.println("nowdate day:"+cal1.get(Calendar.DAY_OF_MONTH)+" enddate day:"+cal2
.get(Calendar.DAY_OF_MONTH));
return isSameDate;
}
}