在之前jdk版本中时间范围获取相对比较复杂。
public static void main(String[] args) throws Exception {
List<String> list = new ArrayList<>();
list.add("2020-04-10 12:20:00");
list.add("2020-04-10 12:00:00");
list.add("abc");
list.add("2020-04-10 12:10:10");
list.add("aaaaaaaaaaaaaaaaaaaa");
list.add("2020-04-10 12:12:10");
list.add("2020-04-10 00:00:10");
String time = "2020-04-10 00:00:00";
SimpleDateFormat format = new SimpleDateFormat("yyyy-dd-MM HH:mm:ss");
Date parse = format.parse(time);
long tx = parse.getTime();
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(tx);
int year = cal.get(Calendar.YEAR);//获取年份
int month=cal.get(Calendar.MONTH);//获取月份
int day=cal.get(Calendar.DATE);//获取日
int hour=cal.get(Calendar.HOUR);//小时
int minute=cal.get(Calendar.MINUTE);//分
int second=cal.get(Calendar.SECOND);//秒
System.out.println(second);
int tl = 0;
if (second==0){
tl = 1000;
if (minute==0){
tl = 60 *1000;
if (hour==0){
tl = 24 * 60 *1000;
}
}
}
int finalTl = tl;
List<String> ls = list.stream().filter(f->getTime(f) && f.length()>18).filter(f->{
try {
System.out.println(tx);
System.out.println(format.parse(f).getTime());
System.out.println(finalTl);
return tx - format.parse(f).getTime() < finalTl;
} catch (ParseException e) {
e.printStackTrace();
}
return false;
}).collect(Collectors.toList());
System.out.println(ls.toString());
}
public static boolean getTime(String time){
SimpleDateFormat format = new SimpleDateFormat("yyyy-dd-MM HH:mm:ss");
try {
format.parse(time);
return true;
} catch (ParseException e) {
e.printStackTrace();
return false;
}
}
jdk8之后对时间处理相对方便了
//获取日期时间
LocalDateTime dateTime = LocalDateTime.now();
//获取日期
LocalDate date = LocalDate.now();
//获取时间
LocalTime time = LocalTime.now();
其中详细后期补充