Java – Check if the date is older than 6 months

This article is showing a few Java examples to check if a Date is older than 30 days or 6 months.

1. LocalDate

1.1 For Localdate, the simplest solution is to use the new Period to check if a LocalDate is older than 6 months.

JavaDate.java

package com.mkyong;

import java.time.LocalDate;
import java.time.Period;

public class JavaDate {

    public static void main(String[] args) {

        LocalDate currentDate = LocalDate.now();
        LocalDate newDate = LocalDate.of(2019, 8, 23);

        System.out.println(currentDate);              // 2020-02-24
        System.out.println(newDate);                  // 2019-08-23

        Period period = Period.between(currentDate, newDate);
        System.out.println(period.getMonths());
        if (period.getMonths() <= -6) {
            System.out.println("date: " + newDate + " is older than 6 months!");
        }
    }

}

Output


2020-02-24
2019-08-23
-6
date: 2019-08-23 is older than 6 months!

1.2 Alternative, create a Localdate and move back to 6 months ago with LocalDate.plusMonths(-6); and uses isBefore() to compare the LocalDate.

JavaDate.java

package com.mkyong;

import java.time.LocalDate;

public class JavaDate {

    public static void main(String[] args) {

        LocalDate currentDate = LocalDate.now();                  

        LocalDate sixMonthsAgo = LocalDate.now().plusMonths(-6);  // 6 months ago

        System.out.println(currentDate);                          // 2020-02-24
        System.out.println(sixMonthsAgo);                         // 2019-08-24

        LocalDate newDate = LocalDate.of(2019, 8, 23);            // 2019-08-23

        // is this date older than 6 months?
        if (newDate.isBefore(sixMonthsAgo)) {
            System.out.println("date: " + newDate + " is older than 6 months!");
        }

    }

}

Output


2020-02-24
2019-08-24
date: 2019-08-23 is older than 6 months!

2. LocalDateTime

This example checks if a LocalDateTime is older than 6 months. For a date containing time, we can use ChronoUnit.{unit}.between.

JavaDate2.java

package com.mkyong;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public class JavaDate2 {

    public static void main(String[] args) {

        LocalDateTime currentDate = LocalDateTime.now();

        LocalDateTime sixMonthsAgo = LocalDateTime.now().plusMonths(-6);

        System.out.println(currentDate);                                // 2020-02-24
        System.out.println(sixMonthsAgo);                               // 2019-08-24

        LocalDateTime newDate = LocalDateTime.of(2019, 8, 23, 0, 0, 0); // 2019-08-23

        // is this date older than 6 months?
        if (newDate.isBefore(sixMonthsAgo)) {
            System.out.println("[isBefore] date: " + newDate + " is older than 6 months!");
        }

        long between = ChronoUnit.MONTHS.between(currentDate, newDate);
        System.out.println(between);
        if (between <= -6) {
            System.out.println("[ChronoUnit] date: " + newDate + " is older than 6 months!");
        }

    }

}

Output


2020-02-24T18:23:05.159116
2019-08-24T18:23:05.159314
[isBefore] date: 2019-08-23T00:00 is older than 6 months!
-6
[ChronoUnit] date: 2019-08-23T00:00 is older than 6 months!

3. ZonedDateTime

3.1 This example checks if a ZonedDateTime is older than 30 days.

JavaDate3

package com.mkyong;

import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;

public class JavaDate3 {

    public static void main(String[] args) {

        ZonedDateTime now = ZonedDateTime.now();         
        ZonedDateTime thirtyDaysAgo = now.plusDays(-30);        // move back 30 days

        System.out.println("now :" + now);                      // 2020-02-24
        System.out.println("thirtyDaysAgo :" + thirtyDaysAgo);  // 2020-01-25

        ZonedDateTime ninetyDaysAgo = now.plusDays(-90);        // move back 90 days
        System.out.println("ninetyDaysAgo :" + ninetyDaysAgo);  // 2019-11-26

        if (ninetyDaysAgo.isBefore(thirtyDaysAgo)) {
            System.out.println("[isBefore()]" + thirtyDaysAgo + " is older than 30 days!");
        }

        long between = ChronoUnit.DAYS.between(now, ninetyDaysAgo);
        System.out.println(between);
        if (between < -30) {
            System.out.println("[ChronoUnit]" + thirtyDaysAgo + " is older than 30 days!");
        }
    }

}

Output


now :2020-02-24T18:39:10.204342+08:00[Asia/Kuala_Lumpur]
thirtyDaysAgo :2020-01-25T18:39:10.204342+08:00[Asia/Kuala_Lumpur]
ninetyDaysAgo :2019-11-26T18:39:10.204342+08:00[Asia/Kuala_Lumpur]

[isBefore()]2020-01-25T18:39:10.204342+08:00[Asia/Kuala_Lumpur] is older than 30 days!
-90
[ChronoUnit]2020-01-25T18:39:10.204342+08:00[Asia/Kuala_Lumpur] is older than 30 days!

4. Calender

4.1 This example checks if a java.util.Calendar is older than 6 months.

JavaDate4.java

package com.mkyong;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class JavaDate4 {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    public static void main(String[] args) {

        Calendar sixMonthsAgo = Calendar.getInstance();             // Current Date
        System.out.println(sdf.format(sixMonthsAgo.getTime()));     // 2020-02-24

        sixMonthsAgo.add(Calendar.MONTH, -6);                       // 6 months ago
        System.out.println(sdf.format(sixMonthsAgo.getTime()));     // 2019-08-24

        Calendar newDate = new GregorianCalendar(2019, 5, 10);      // 2019-06-10

        if (newDate.before(sixMonthsAgo)) {
            System.out.println("newDate : " + sdf.format(newDate.getTime()) + " is older than 6 months");
        }


    }

}

Output


2020-02-24
2019-08-24
newDate : 2019-06-10 is older than 6 months

5. Date

5.1 This example checks if a java.util.Date is older than 30 days.

JavaDate5.java

package com.mkyong;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class JavaDate5 {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    public static void main(String[] args) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date today = new Date();
        System.out.println("today : " + sdf.format(today));         // 2020-02-24

        Calendar thirtyDaysAgo = Calendar.getInstance();
        thirtyDaysAgo.add(Calendar.DAY_OF_MONTH, -30);             // 2020-01-25

        Date thirtyDaysAgoDate = thirtyDaysAgo.getTime();
        System.out.println("thirtyDaysAgo : " + sdf.format(thirtyDaysAgoDate));

        Date newDate = sdf.parse("2009-12-31");

        if (newDate.before(thirtyDaysAgoDate)) {
            System.out.println(sdf.format(newDate) + " is older than 30 days!");
        }

    }

}

Output


today : 2020-02-24
thirtyDaysAgo : 2020-01-25
2009-12-31 is older than 30 days!

References

author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar