How do I count number of weekday between two dates?

The code below helps you find the number of a specified weekday (Monday, Tuesday, Wednesday, etc.) between two dates. The solution we used below is to loop between the two dates and check if the weekday of those dates are equals to the day we want to count.

package org.kodejava.util;

import java.util.Calendar;

public class DaysBetweenDate {
    public static void main(String[] args) {
        Calendar start = Calendar.getInstance();
        start.set(2021, Calendar.OCTOBER, 1);
        Calendar end = Calendar.getInstance();
        end.set(2021, Calendar.OCTOBER, 31);

        System.out.print("Number Monday between " +
                start.getTime() + " and " + end.getTime() + " are: ");

        int numberOfDays = 0;
        while (start.before(end)) {
            if (start.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
                numberOfDays++;
                start.add(Calendar.DATE, 7);
            } else {
                start.add(Calendar.DATE, 1);
            }
        }

        System.out.println(numberOfDays);
    }
}

The result of our program is:

Number Monday between Fri Oct 01 07:55:06 CST 2021 and Sun Oct 31 07:55:06 CST 2021 are: 4

How do I add hours, minutes or seconds to a date?

This example shows you how to add or subtract hours, minutes or seconds to a date using the java.util.Calendar object.

package org.kodejava.util;

import java.util.Calendar;

public class DateAddSubtract {
    public static void main(String[] args) {
        // Gets a calendar using the default time zone and locale. The
        // Calendar returned is based on the current time in the default
        // time zone with the default locale.
        Calendar calendar = Calendar.getInstance();
        System.out.println("Original = " + calendar.getTime());

        // Subtract 2 hour from the current time
        calendar.add(Calendar.HOUR, -2);

        // Add 30 minutes to the calendar time
        calendar.add(Calendar.MINUTE, 30);

        // Add 300 seconds to the calendar time
        calendar.add(Calendar.SECOND, 300);
        System.out.println("Updated  = " + calendar.getTime());
    }
}

The output of the code snippet:

Original = Wed Oct 06 19:47:53 CST 2021
Updated  = Wed Oct 06 18:22:53 CST 2021

How do I know if a date is after another date?

This example demonstrate Date‘s class after() method to check if a date is later than another date.

package org.kodejava.util;

import java.util.Date;
import java.util.Calendar;

public class DateCompareAfter {
    public static void main(String[] args) {
        // Get current date
        Date today = new Date();

        // Add 1 day from the current date.
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, 1);
        Date tomorrow = calendar.getTime();

        // Tests if this date is after the specified date. This method will
        // return true if the value time represented by the tomorrow object
        // is later than today.
        if (tomorrow.after(today)) {
            System.out.println(tomorrow + " is after " + today);
        }
    }
}

The result of the code snippet above is:

Tue Oct 05 20:52:34 CST 2021 is after Mon Oct 04 20:52:33 CST 2021

How do I know if a date is before another date?

This example demonstrate Date‘s class before() method to check if a date is earlier than another date.

package org.kodejava.util;

import java.util.Date;
import java.util.Calendar;

public class DateCompareBefore {
    public static void main(String[] args) {
        // Get current date
        Date today = new Date();

        // Subtract 1 day from the current date.
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        Date yesterday = calendar.getTime();

        // Tests if this date is before the specified date. This method will
        // return true if the value time represented by the yesterday object
        // is earlier than today.
        if (yesterday.before(today)) {
            System.out.println(yesterday + " is before " + today);
        }
    }
}

The result of the code snippet above is:

Sun Oct 03 20:49:36 CST 2021 is before Mon Oct 04 20:49:36 CST 2021

How do I compare two dates?

In this example you’ll see the utilization of SimpleDateFormat class for comparing two dates for equality. We convert the date object into string using the DateFormat instance and then compare it using String.equals() method.

package org.kodejava.util;

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

public class ComparingDate {
    public static void main(String[] args) {
        // Create a SimpleDateFormat instance with dd/MM/yyyy format
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

        // Get the current date
        Date today = new Date();

        // Create an instance of calendar that represents a new year date
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        calendar.set(Calendar.YEAR, 2021);

        String newYear = df.format(calendar.getTime());
        String now = df.format(today);

        // Using the string equals method we can compare the date.
        if (now.equals(newYear)) {
            System.out.println("Happy New Year!");
        } else {
            System.out.println("Have a nice day!");
        }
    }
}