Java获取前三个月的数据(以当前时间为基准)
public static void main(String[] args) {
Date dNow = new Date(); //当前时间
Date dBefore = new Date();
Calendar calendar = Calendar.getInstance(); //得到日历
calendar.setTime(dNow);//把当前时间赋给日历
calendar.add(Calendar.MONTH, -3); //设置为前3月
dBefore = calendar.getTime(); //得到前3月的时间
System.out.println("前三个月的时间戳为:"+dBefore.getTime());
System.out.println("当前时间减去前三个月的时间差");
System.out.println(dNow.getTime()-dBefore.getTime());
long strarTm = 1056486041591L;
if ((dNow.getTime()-strarTm)>dNow.getTime()-dBefore.getTime()){
System.out.println("数据不是前三个月的数据");
}else{
//可放入相对应的数据
System.out.println("数据是前三个月的数据");
}
}