Java – Check if the date is older than 30 days or 6 months
This article shows Java 8 and legacy date-time APIs example to check if a date is 30 days or 6 months older than the current date.
- 1. Java 8 isBefore()
- 2. Java 8 ChronoUnit.{UNIT}.between()
- 3. Java 8 Period.between()
- 4. Legacy Calendar and Date
- 5. References
1. Java 8 isBefore()
First minus the current date and then uses isBefore()
to check if a date is a certain period older than the current date.
1.1 This example checks if a date is 6 months older than the current date.
package com.mkyong.app;
import java.time.LocalDate;
public class JavaMinusMonths {
public static void main(String[] args) {
LocalDate currentDate = LocalDate.now();
LocalDate currentDateMinus6Months = currentDate.minusMonths(6);
// 2021-03-26
System.out.println("currentDate: " + currentDate);
// 2020-09-26
System.out.println("currentDateMinus6Months : " + currentDateMinus6Months);
LocalDate date1 = LocalDate.of(2019, 8, 23);
System.out.println("\ndate1 : " + date1);
if (date1.isBefore(currentDateMinus6Months)) {
System.out.println("6 months older than current date!");
}
}
}
Output
currentDate: 2021-03-26
currentDateMinus6Months : 2020-09-26
date1 : 2019-08-23
6 months older than current date!
1.2 This example checks if a date is 30 days older than the current date.
package com.mkyong.app;
import java.time.LocalDate;
public class JavaMinusDays {
public static void main(String[] args) {
LocalDate currentDate = LocalDate.now();
LocalDate currentDateMinus30Days = currentDate.minusDays(30);
// 2021-03-26
System.out.println("currentDate: " + currentDate);
// 2020-09-26
System.out.println("currentDateMinus30Days : " + currentDateMinus30Days);
LocalDate date1 = LocalDate.of(2019, 8, 23);
System.out.println("\ndate1 : " + date1);
if (date1.isBefore(currentDateMinus30Days)) {
System.out.println("30 months older than current date!");
}
}
}
2. Java 8 ChronoUnit.{UNIT}.between()
Majority new Java 8 date and time APIs are implemented the Temporal
interface, for examples, LocalDate
, LocalDateTime
, ZonedDateTime
, etc. We can use the ChronoUnit.{UNIT}.between
to check the difference between two dates or Temporal
objects.
2.1 Review the following ChronoUnit.{UNIT}.between
methods:
ChronoUnit.NANOS.between(Temporal t1, Temporal t2)
ChronoUnit.MICROS.between(Temporal t1, Temporal t2)
ChronoUnit.MILLIS.between(Temporal t1, Temporal t2)
ChronoUnit.SECONDS.between(Temporal t1, Temporal t2)
ChronoUnit.MINUTES.between(Temporal t1, Temporal t2)
ChronoUnit.HOURS.between(Temporal t1, Temporal t2)
ChronoUnit.HALF_DAYS.between(Temporal t1, Temporal t2)
ChronoUnit.DAYS.between(Temporal t1, Temporal t2)
ChronoUnit.WEEKS.between(Temporal t1, Temporal t2)
ChronoUnit.MONTHS.between(Temporal t1, Temporal t2)
ChronoUnit.YEARS.between(Temporal t1, Temporal t2)
ChronoUnit.DECADES.between(Temporal t1, Temporal t2)
ChronoUnit.CENTURIES.between(Temporal t1, Temporal t2)
ChronoUnit.MILLENNIA.between(Temporal t1, Temporal t2)
ChronoUnit.ERAS.between(Temporal t1, Temporal t2)
2.2 This example uses ChronoUnit.MONTHS.between
to check if a date is 6 months older than the current date.
package com.mkyong.app;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class JavaChronoUnit1 {
public static void main(String[] args) {
LocalDate now = LocalDate.now(); // 2021-03-26
System.out.println("now: " + now);
LocalDate date1 = LocalDate.of(2019, 9, 25);
long months = ChronoUnit.MONTHS.between(now, date1);
System.out.println(date1);
System.out.println(months);
if (months <= -6) {
System.out.println("6 months older than current date!");
}
LocalDate date2 = LocalDate.of(2020, 9, 26);
long months2 = ChronoUnit.MONTHS.between(now, date2);
System.out.println(date2);
System.out.println(months2);
if (months2 <= -6) {
System.out.println("6 months older than current date!");
}
}
}
Output
now: 2021-03-26
2019-09-25
-18
6 months older than current date!
2020-09-26
-6
6 months older than current date!
2.3 This example uses ChronoUnit.DAYS.between
to check if a date 30 days older than the current date.
package com.mkyong.app;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class JavaChronoUnit2 {
public static void main(String[] args) {
LocalDate now = LocalDate.now(); // 2021-03-26
System.out.println("now: " + now);
LocalDate date1 = LocalDate.of(2019, 9, 25);
long day1 = ChronoUnit.DAYS.between(now, date1);
System.out.println(date1);
System.out.println(day1);
if (day1 <= -30) {
System.out.println("30 days older than current date!");
}
LocalDate date2 = LocalDate.of(2021, 2, 25);
long day2 = ChronoUnit.DAYS.between(now, date2);
System.out.println(date2);
System.out.println(day2);
if (day2 <= -30) {
System.out.println("30 days older than current date!");
}
}
}
Output
now: 2021-03-26
2019-09-25
-548
30 days older than current date!
2021-02-25
-29
3. Java 8 Period.between()
3.1 Read a simple java.time.Period
example to understand how the period works.
package com.mkyong.app;
import java.time.LocalDate;
import java.time.Period;
public class JavaPeriodExample {
public static void main(String[] args) {
LocalDate date1 = LocalDate.of(2020, 2, 24);
LocalDate date2 = LocalDate.of(2019, 8, 23);
System.out.println(date1); // 2020-02-24
System.out.println(date2); // 2019-08-23
Period period = Period.between(date1, date2);
String result = String.format("%d years, %d months, %d days",
period.getYears(), period.getMonths(), period.getDays());
System.out.println(result); // 0 years, -6 months, -1 days
LocalDate date3 = LocalDate.of(2019, 1, 1);
LocalDate date4 = LocalDate.of(2021, 3, 23);
System.out.println(date3); // 2019-01-01
System.out.println(date4); // 2021-03-23
Period period2 = Period.between(date3, date4);
String result2 = String.format("%d years, %d months, %d days",
period2.getYears(), period2.getMonths(), period2.getDays());
System.out.println(result2); // 2 years, 2 months, 22 days
}
}
Output
2020-02-24
2019-08-23
0 years, -6 months, -1 days
2019-01-01
2021-03-23
2 years, 2 months, 22 days
3.2 This example uses Period
to check if a LocalDate
is 6 months older than the current date.
package com.mkyong.app;
import java.time.LocalDate;
import java.time.Period;
public class JavaPeriodExample2 {
public static void main(String[] args) {
LocalDate date1 = LocalDate.of(2020, 9, 25);
System.out.println("Is 6 months older? " + isOlderThanMonths(date1, 6));
LocalDate date2 = LocalDate.of(2020, 9, 26);
System.out.println("Is 6 months older? " + isOlderThanMonths(date2, 6));
LocalDate date3 = LocalDate.of(2020, 10, 26);
System.out.println("Is 6 months older? " + isOlderThanMonths(date3, 6));
LocalDate date4 = LocalDate.of(2001, 10, 26);
System.out.println("Is 6 months older? " + isOlderThanMonths(date4, 6));
}
static boolean isOlderThanMonths(final LocalDate date, final int months) {
boolean result = false;
LocalDate now = LocalDate.now();
// period from now to date
Period period = Period.between(now, date);
System.out.println("\nNow: " + now);
System.out.println("Date: " + date);
System.out.printf("%d years, %d months, %d days%n",
period.getYears(), period.getMonths(), period.getDays());
if (period.getYears() < 0) {
// if year is negative, 100% older than 6 months
result = true;
} else if (period.getYears() == 0) {
if (period.getMonths() <= -months) {
result = true;
}
}
return result;
}
}
Output
Now: 2021-03-26
Date: 2020-09-25
0 years, -6 months, -1 days
Is 6 months older? true
Now: 2021-03-26
Date: 2020-09-26
0 years, -6 months, 0 days
Is 6 months older? true
Now: 2021-03-26
Date: 2020-10-26
0 years, -5 months, 0 days
Is 6 months older? false
Now: 2021-03-26
Date: 2001-10-26
-19 years, -5 months, 0 days
Is 6 months older? true
4. Legacy Calendar and Date
4.1 This example checks if a java.util.Calendar
is 6 months older than the current date.
package com.mkyong.app;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class JavaCalendarExample {
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
public static void main(String[] args) {
Calendar sixMonthsAgo = Calendar.getInstance();
// Current date
// 2021-03-26
System.out.println("now: " + sdf.format(sixMonthsAgo.getTime()));
// old way to minus 6 months
// 2020-09-26
sixMonthsAgo.add(Calendar.MONTH, -6);
System.out.println("sixMonthsAgo: " + sdf.format(sixMonthsAgo.getTime()));
// 2019-06-10
Calendar date1 = new GregorianCalendar(2020, Calendar.AUGUST, 10);
System.out.println("date1: " + sdf.format(date1.getTime()));
if (date1.before(sixMonthsAgo)) {
System.out.println("6 months older than current date!");
}
}
}
Output
now: 2021-03-26
sixMonthsAgo: 2020-09-26
date1: 2020-08-10
6 months older than current date!
4.2 This example checks if a java.util.Date
is 30 days older than the current date.
package com.mkyong.app;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class JavaDateExample {
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
public static void main(String[] args) throws ParseException {
Date today = new Date();
System.out.println("now : " + sdf.format(today)); // 2021-03-26
Calendar thirtyDaysAgo = Calendar.getInstance();
thirtyDaysAgo.add(Calendar.DAY_OF_MONTH, -30); // 2021-02-24
// convert Calendar to Date
Date thirtyDaysAgoDate = thirtyDaysAgo.getTime();
System.out.println("thirtyDaysAgo : " + sdf.format(thirtyDaysAgoDate));
Date date1 = sdf.parse("2019-12-31");
if (date1.before(thirtyDaysAgoDate)) {
System.out.println("30 days older than current date!");
}
}
}
Output
now : 2021-03-26
thirtyDaysAgo : 2021-02-24
30 days older than current date!
Hi!
Thank you for all these tutorials!
In the JavaPeriodExample2 wouldn’t fit better the name isMonthsOrOlder or isXMonthsOrOlder method name than the isOlderThanMonths?
Or another approach is to leave the method name (isOlderThanMonths) but change the logic and return true only if the days for the period are negative as well. Like:
….
else if (period.getYears() == 0) {
if (period.getMonths() <= -months) {
result = period.getDays() < 0;
}
}
Keep them coming! 🙂
B.