一、问题描述
今天查询一个bug发现了这个问题,使用Calendar 中的before 和after方法比较日期是否在范围内,实际与代码不符,我就去查明原因。
首先说下这两个方法吧。
日期1.before(日期2),第一个日期在第二个日期之前的意思。
日期1.after(日期2),第一个日期在第二个日期之后的意思。
代码逻辑:
//获取当前时间
Calendar now = Calendar.getInstance();
now.setTime(new Date());
//获取今年开始时间
Calendar nowBegin = Calendar.getInstance();
nowBegin.set(now.get(Calendar.YEAR),1,1,0,0,0);
//获取要判断的时间
Calendar talkTime = Calendar.getInstance()
talkTime.setTime(dto.getTalkTime());
//判断今年是否有谈话记录
if(talkTime.before(now)&&talkTime.after(nowBegin)){
return true;
}
测试中发现一条一月的数据没有被检查到,经分析是nowBegin的时间不对,debug后发现nowBegin的月份是2月。
二、问题分析
发现问题后就是解决问题,去看了下Calendar的源代码。
/**
* Value of the {@link #MONTH} field indicating the
* first month of the year in the Gregorian and Julian calendars.
*/
public final static int JANUARY = 0;
/**
* Value of the {@link #MONTH} field indicating the
* second month of the year in the Gregorian and Julian calendars.
*/
public final static int FEBRUARY = 1;
/**
* Value of the {@link #MONTH} field indicating the
* third month of the year in the Gregorian and Julian calendars.
*/
public final static int MARCH = 2;
/**
* Value of the {@link #MONTH} field indicating the
* fourth month of the year in the Gregorian and Julian calendars.
*/
public final static int APRIL = 3;
/**
* Value of the {@link #MONTH} field indicating the
* fifth month of the year in the Gregorian and Julian calendars.
*/
public final static int MAY = 4;
/**
* Value of the {@link #MONTH} field indicating the
* sixth month of the year in the Gregorian and Julian calendars.
*/
public final static int JUNE = 5;
/**
* Value of the {@link #MONTH} field indicating the
* seventh month of the year in the Gregorian and Julian calendars.
*/
public final static int JULY = 6;
/**
* Value of the {@link #MONTH} field indicating the
* eighth month of the year in the Gregorian and Julian calendars.
*/
public final static int AUGUST = 7;
/**
* Value of the {@link #MONTH} field indicating the
* ninth month of the year in the Gregorian and Julian calendars.
*/
public final static int SEPTEMBER = 8;
/**
* Value of the {@link #MONTH} field indicating the
* tenth month of the year in the Gregorian and Julian calendars.
*/
public final static int OCTOBER = 9;
/**
* Value of the {@link #MONTH} field indicating the
* eleventh month of the year in the Gregorian and Julian calendars.
*/
public final static int NOVEMBER = 10;
/**
* Value of the {@link #MONTH} field indicating the
* twelfth month of the year in the Gregorian and Julian calendars.
*/
public final static int DECEMBER = 11;
通过上面可以看出,月份对应的数字是减1的,所以在set月份时需减一处理。所以导致一月的数据一直检索不到。
三、解决方式
//set月份
calendar.set(Calendar.MONTH, Integer.valueOf(split[1]) - 1);
//获取今年开始时间
Calendar nowBegin = Calendar.getInstance();
nowBegin.set(now.get(Calendar.YEAR),0,1,0,0,0);
四、补充
除了月份外,我顺便看了下Calendar类中也定义的一些关于星期的常量,如下:
/**
* Value of the {@link #DAY_OF_WEEK} field indicating
* Sunday.
*/
public final static int SUNDAY = 1;
/**
* Value of the {@link #DAY_OF_WEEK} field indicating
* Monday.
*/
public final static int MONDAY = 2;
/**
* Value of the {@link #DAY_OF_WEEK} field indicating
* Tuesday.
*/
public final static int TUESDAY = 3;
/**
* Value of the {@link #DAY_OF_WEEK} field indicating
* Wednesday.
*/
public final static int WEDNESDAY = 4;
/**
* Value of the {@link #DAY_OF_WEEK} field indicating
* Thursday.
*/
public final static int THURSDAY = 5;
/**
* Value of the {@link #DAY_OF_WEEK} field indicating
* Friday.
*/
public final static int FRIDAY = 6;
/**
* Value of the {@link #DAY_OF_WEEK} field indicating
* Saturday.
*/
public final static int SATURDAY = 7;
如果想通过calendar.get(Calendar.DAY_OF_WEEK);获取指定日期是星期几也需要特别处理。
比如想获取2023年3月7日是星期几,通过这种方式获取到的将是3,实际上这天是星期二
//获取当前时间
Calendar now = Calendar.getInstance();
now.set(Calendar.YEAR,2023);
now.set(Calendar.MONTH,Calendar.MARCH);
now.set(Calendar.DATE,7);
int week = now.get(Calendar.DAY_OF_WEEK);
System.out.printIn("week:"+week);
最后打印出来是:week:1