How do I get the last day of a month?

package org.kodejava.util;

import java.text.DateFormatSymbols;
import java.util.Calendar;

public class LastDayOfMonth {
    public static void main(String[] args) {
        // Get a calendar instance
        Calendar calendar = Calendar.getInstance();

        // Get the last date of the current month. To get the last date for a
        // specific month you can set the calendar month using calendar object
        // calendar.set(Calendar.MONTH, theMonth) method.
        int lastDate = calendar.getActualMaximum(Calendar.DATE);

        // Set the calendar date to the last date of the month so then we can
        // get the last day of the month
        calendar.set(Calendar.DATE, lastDate);
        int lastDay = calendar.get(Calendar.DAY_OF_WEEK);

        // Print the current date and the last date of the month
        System.out.println("Last Date: " + calendar.getTime());

        // The lastDay will be in a value from 1 to 7 where 1 = Sunday and 7 =
        // Saturday. The first day of the week is based on the locale.
        System.out.println("Last Day : " + lastDay);

        // Get weekday name
        DateFormatSymbols dfs = new DateFormatSymbols();
        System.out.println("Last Day : " + dfs.getWeekdays()[lastDay]);
    }
}

Here is the output of the code snippet above:

Last Date: Thu Sep 30 06:44:05 CST 2021
Last Day : 5
Last Day : Thursday

How do I convert day-of-year to day-of-month?

package org.kodejava.util;

import java.util.Calendar;

public class DayYearToDayMonth {
    public static void main(String[] args) {
        // Create an instance of calendar for the year 2017 and set the
        // day to the 180 day of the year.
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, 2021);
        cal.set(Calendar.DAY_OF_YEAR, 180);

        // Print the date of the calendar.
        System.out.println("Calendar date is: " + cal.getTime());

        // To know what day in month of the calendar we can obtain the
        // value by calling Calendar's instance get() method and pass
        // the Calendar.DAY_OF_MONTH field.
        int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);

        // Print which month day is it in number.
        System.out.println("Calendar day of month: " + dayOfMonth);

        // To know what day in week of the calendar we can obtain the
        // value by calling Calendar's instance get() method and pass
        // the Calendar.DAY_OF_WEEK field.
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

        // Print which week day is it in number.
        System.out.println("Calendar day of week: " + dayOfWeek);
    }
}

The result of our above example is.

Calendar date is: Tue Jun 29 08:14:06 CST 2021
Calendar day of month: 29
Calendar day of week: 3

How do I get day of week?

In this example we want to create a calendar or date from a year and day of the year. Next we will find out what day-of-week that calendar or date represent. In the code snippet below we are trying to find the 180 day of the year 2021.

Let’s see the example below.

package org.kodejava.util;

import java.text.DateFormatSymbols;
import java.util.Calendar;

public class DayOfYearToDayOfWeekExample {
    public static void main(String[] args) {
        // Create a calendar with year and day of year.
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, 2021);
        calendar.set(Calendar.DAY_OF_YEAR, 180);

        // See the full information of the calendar object.
        System.out.println(calendar.getTime().toString());

        // Get the weekday and print it
        int weekday = calendar.get(Calendar.DAY_OF_WEEK);
        System.out.println("Weekday: " + weekday);

        // Get weekday name
        DateFormatSymbols dfs = new DateFormatSymbols();
        System.out.println("Weekday: " + dfs.getWeekdays()[weekday]);
    }
}

Below is the result of our program.

Tue Jun 29 06:04:21 CST 2021
Weekday: 3
Weekday: Tuesday