java 检查日期是否大于 10 年和大于 20 年
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34014651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if date is older than 10 years and newer than 20 years
提问by Andy897
I am trying to check in Java 8 if a date is older than 10 years and newer than 20 years. I am using Date.before()And Date.after()and passing currentDate-10years and currentDate-20years as arguments.
如果日期早于 10 年且晚于 20 年,我正在尝试检查 Java 8。我使用Date.before()并Date.after()和传球currentDate-10年,currentDate-20年作为参数。
Can someone please suggest what will the cleanest way to get a date which is 10 year old and 20 years old in Date format to pass it in my before()and after()methods?
有人可以请建议会出现什么干净的方式来得到一个日期是10岁的和日期格式20岁的它掠过我before()和after()方法呢?
采纳答案by Ramesh-X
Using Calendaryou can easily get a 10 year old date and 20 year old date from the current date.
使用Calendar您可以轻松地从当前日期获得 10 岁的日期和 20 岁的日期。
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, -10);
Date d1 = calendar.getTime();
calendar.add(Calendar.YEAR, -10);
Date d2 = calendar.getTime();
As you are using Java 8 you can also use LocalDate
当您使用 Java 8 时,您也可以使用 LocalDate
LocalDate currentDate = LocalDate.now();
Date d1 = Date.from(currentDate.minusYears(10).atStartOfDay(ZoneId.systemDefault()).toInstant());
Date d2 = Date.from(currentDate.minusYears(20).atStartOfDay(ZoneId.systemDefault()).toInstant());
For comparing you can use the date.after()and date.before()methods as you said.
为了进行比较,您可以使用您所说的date.after()和date.before()方法。
if(date.after(d1) && date.before(d2)){ //date is the Date instance that wants to be compared
////
}
The before()and after()methods are implemented in Calendarand LocalDatetoo. You can use those methods in those instances without converting into java.util.Dateinstances.
该before()和after()方法在实施Calendar和LocalDate太。您可以在那些实例中使用这些方法,而无需转换为java.util.Date实例。
回答by dkulkarni
You can use java.time.LocalDateto do this. Example: If you need to check if 01/01/2005 is between that duration, you can use
您可以使用java.time.LocalDate来执行此操作。示例:如果您需要检查 01/01/2005 是否在该持续时间之间,您可以使用
LocalDate date = LocalDate.of(2005, 1, 1); // Assign date to check
LocalDate today = LocalDate.now();
if (date.isBefore(today.minusYears(10)) && date.isAfter(today.minusYears(20))) {
//Do Something
}
回答by Tunaki
Another possibility is to get the year count between the date to check and the upper date. If the number of year is greater than 0 and less than 10, it means the date to check is older than 10 years and newer than 20 years.
另一种可能性是获取要检查的日期和上一个日期之间的年份计数。如果年数大于 0 小于 10,则表示要检查的日期大于 10 年且小于 20 年。
This code will determine any date in the interval ]now - 20 years ; now - 10 years[:
此代码将确定时间间隔中的任何日期]now - 20 years ; now - 10 years[:
public static void main(String[] args) {
LocalDate dateToCheck = LocalDate.now().minusYears(20).plusDays(1);
LocalDate upperYear = LocalDate.now().minusYears(10);
long yearCount = ChronoUnit.YEARS.between(dateToCheck, upperYear);
if (yearCount > 0 && yearCount < 10) {
System.out.println("date is older than 10 years and newer than 20 years");
}
}

