Home » Programming » Java » How to find the date of coming Sunday Monday or any day

How to find the date of coming Sunday Monday or any day

This article explains how to find the date of coming Sunday, Monday, Tuesday, or any day by using java. Here I used a calendar from java 7 to find the date.

Before going into the program let me say a small example of usage of this program. In the eCommerce platform, some of the retailers deliver their products to their customers on a particular day only. So in this situation, we need this kind of program to find a delivery date.

Actually, there are many circumstances in a project we need to do this kind of calculation. So this program is a key to that requirements.

How to find the date of coming Sunday

Kindly go through the below snippet. It will return the date of coming Sunday.

public static Date comingSundayDate() {
    Calendar calendar = Calendar.getInstance();
    int currentDay = calendar.get(Calendar.DAY_OF_WEEK);
    int dayDifference = Calendar.SUNDAY - currentDay;
    if (dayDifference <= 0) {       
        dayDifference += 7;
    }
    calendar.add(Calendar.DAY_OF_YEAR, dayDifference);
    return calendar.getTime();
}

Let me explain the program how it works. First, we need to set the current date on the calendar.

Calendar calendar = Calendar.getInstance() - This line set the current date instance to the calendar.

calendar.get(Calendar.DAY_OF_WEEK) - This line produces the day of the week in integer format like 1,2,3..etc. Actually, Sunday is 1 and all the consecutive days are having consecutive numbers. At last, Saturday is 7.

Then we need to subtract the current day(currentDay) from the day of Sunday(Calendar.SUNDAY). If the result is less than or equal to 0 then we have to add 7 days.

Finally, we have to add the resultant days(dayDifference) to Calendar.DAY_OF_YEAR by using calendar.add() function. Then It returns the exact date of the coming Sunday. The sample output is like "Sun Sep 06 22:07:52 IST 2020".

If we want to have the display format of date then you please use Calendar calendar = Calendar.getInstance() - This line sets the current date instance to the calendar.to convert the date in any format whichever you want. Below is the sample for the same.

SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
String displayDate = dateFormatter.format(calendar.getTime());
System.out.println(displayDate);

The above changes return the date "Sun Sep 06 22:07:52 IST 2020" to display date "06/09/2020".

How to find the date of coming Monday

This program explains how to find the date of coming Monday. Actually the above program works this for also but we need to do slight changes to that.

Yes, Of-course let us see what changes we have to do. Actually, you can see this line "int dayDifference = Calendar.SUNDAY - currentDay;" in the above program for finding coming Sunday. Right!. this dayDifference value always negative or zero because Sunday is the least day in a week.

But finding the date of coming Monday or any day from Monday to Saturday the dayDifference value in the program becomes positive or negative. So for this change, we need to add a little bit of code to the above program.

The below snippet is used to find the date of coming Monday.

public static Date comingMondayDate() {
    Calendar calendar = Calendar.getInstance();
    int currentDay = calendar.get(Calendar.DAY_OF_WEEK);
    int dayDifference = Calendar.MONDAY - currentDay;
    if (dayDifference > 0) {
        calendar.add(Calendar.DAY_OF_YEAR, dayDifference);
    }
    if (dayDifference <= 0) {
        dayDifference += 7;
        calendar.add(Calendar.DAY_OF_YEAR, dayDifference);
    }
    return calendar.getTime();
}

Now we can see the difference between the above two programs. For the positive case of dayDifference value, I added some set of code that's it. We can use this same program for finding the date of any coming day between Monday to Saturday.

The only one line differs in all the programs. Yes this line only "int dayDifference = Calendar.MONDAY - currentDay". If we want to find the date of coming Monday then we need to use Calendar.MONDAY, for Tuesday Calendar.TUESDAY accordingly it will change.

How to find the date of coming Tuesday

The below snippet is used for finding the date of coming Tuesday.

public static Date comingTuesdayDate() {
    Calendar calendar = Calendar.getInstance();
    int currentDay = calendar.get(Calendar.DAY_OF_WEEK);
    int dayDifference = Calendar.TUESDAY- currentDay;
    if (dayDifference > 0) {
        calendar.add(Calendar.DAY_OF_YEAR, dayDifference);
    }
    if (dayDifference <= 0) {
        dayDifference += 7;
        calendar.add(Calendar.DAY_OF_YEAR, dayDifference);
    }
    return calendar.getTime();
}

How to find the date of coming Wednesday

The below snippet is used for finding the date of coming Wednesday

public static Date comingWednesdayDate() {
    Calendar calendar = Calendar.getInstance();
    int currentDay = calendar.get(Calendar.DAY_OF_WEEK);
    int dayDifference = Calendar.WEDNESDAY- currentDay;
    if (dayDifference > 0) {
        calendar.add(Calendar.DAY_OF_YEAR, dayDifference);
    }
    if (dayDifference <= 0) {
        dayDifference += 7;
        calendar.add(Calendar.DAY_OF_YEAR, dayDifference);
    }
    return calendar.getTime();
}

How to find the date of coming Thursday

The below snippet is used for finding the date of coming Thursday

public static Date comingThursdayDate() {
    Calendar calendar = Calendar.getInstance();
    int currentDay = calendar.get(Calendar.DAY_OF_WEEK);
    int dayDifference = Calendar.THURSDAY- currentDay;
    if (dayDifference > 0) {
        calendar.add(Calendar.DAY_OF_YEAR, dayDifference);
    }
    if (dayDifference <= 0) {
        dayDifference += 7;
        calendar.add(Calendar.DAY_OF_YEAR, dayDifference);
    }
    return calendar.getTime();
}

How to find the date of coming Friday

The below snippet is used for finding the date of coming Friday

public static Date comingFridayDate() {
    Calendar calendar = Calendar.getInstance();
    int currentDay = calendar.get(Calendar.DAY_OF_WEEK);
    int dayDifference = Calendar.FRIDAY- currentDay;
    if (dayDifference > 0) {
        calendar.add(Calendar.DAY_OF_YEAR, dayDifference);
    }
    if (dayDifference <= 0) {
        dayDifference += 7;
        calendar.add(Calendar.DAY_OF_YEAR, dayDifference);
    }
    return calendar.getTime();
}

How to find the date of coming Saturday

The below snippet is used for finding the date of coming Saturday

public static Date comingSaturdayDate() {
    Calendar calendar = Calendar.getInstance();
    int currentDay = calendar.get(Calendar.DAY_OF_WEEK);
    int dayDifference = Calendar.SATURDAY- currentDay;
    if (dayDifference > 0) {
        calendar.add(Calendar.DAY_OF_YEAR, dayDifference);
    }
    if (dayDifference <= 0) {
        dayDifference += 7;
        calendar.add(Calendar.DAY_OF_YEAR, dayDifference);
    }
    return calendar.getTime();
}

I hope this program is useful. In case of queries please type your comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *