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

How do I get number of days in a month?

Let say you want to know the number of days in a month, or we can say it as the last date of a month. The example below shows you how to obtain the number of days or the date.

package org.kodejava.util;

import java.util.Calendar;

public class MonthDaysExample {
    public static void main(String[] args) {
        // First get an instance of calendar object.
        Calendar calendar = Calendar.getInstance();

        // We'll set the date of the calendar to the following
        // date. We can use constant variable in the calendar
        // for months value (JANUARY - DECEMBER). Be informed that
        // month in Java started from 0 instead of 1.
        int year = 2021;
        int month = Calendar.FEBRUARY;
        int date = 1;
        // We have a new date of 2021-02-01
        calendar.set(year, month, date);

        // Here we get the maximum days for the date specified
        // in the calendar. In this case we want to get the number
        // of days for february 2021
        int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        System.out.println("Max Day: " + maxDay);

        // Here we want to see what is the days for february on
        // a leap year.
        calendar.set(2020, Calendar.FEBRUARY, 1);
        maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        System.out.println("Max Day: " + maxDay);
    }
}

The result of the code snippet above:

Max Day: 28
Max Day: 29

In JDK 8 you can use the new Date Time API to get the number of days in a month. Here an example that show you how to do it: How do I get the length of month represented by a date object?.

How do I add or subtract a date?

The java.util.Calendar allows us to do a date arithmetic function such as add or subtract a unit of time to the specified date field.

The method that done this process is the Calendar.add(int field, int amount). Where the value of the field can be Calendar.DATE, Calendar.MONTH, Calendar.YEAR. So this mean if you want to subtract in days, months or years use Calendar.DATE, Calendar.MONTH or Calendar.YEAR respectively.

package org.kodejava.util;

import java.util.Calendar;

public class CalendarAddExample {
    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();

        System.out.println("Today : " + cal.getTime());

        // Subtract 30 days from the calendar
        cal.add(Calendar.DATE, -30);
        System.out.println("30 days ago: " + cal.getTime());

        // Add 10 months to the calendar
        cal.add(Calendar.MONTH, 10);
        System.out.println("10 months later: " + cal.getTime());

        // Subtract 1 year from the calendar
        cal.add(Calendar.YEAR, -1);
        System.out.println("1 year ago: " + cal.getTime());
    }
}

In the code above we want to know what is the date back to 30 days ago. The sample result of the code is shown below:

Today : Wed Sep 15 17:58:49 CST 2021
30 days ago: Mon Aug 16 17:58:49 CST 2021
10 months later: Thu Jun 16 17:58:49 CST 2022
1 year ago: Wed Jun 16 17:58:49 CST 2021

How do I get the current month name?

To get the current month name from the system we can use java.util.Calendar class. The Calendar.get(Calendar.MONTH) returns the month value as an integer starting from 0 as the first month and 11 as the last month. This mean January equals to 0, February equals to 1 and December equals to 11.

Let’s see the code below:

package org.kodejava.example.util;

import java.util.Calendar;

public class GetMonthNameExample {
    public static void main(String[] args) {
        String[] monthName = {"January", "February",
                "March", "April", "May", "June", "July",
                "August", "September", "October", "November",
                "December"};

        Calendar cal = Calendar.getInstance();
        String month = monthName[cal.get(Calendar.MONTH)];

        System.out.println("Month name: " + month);
    }
}

On the first line inside the main method we declare an array of string that keep our month names. Next we get the integer value of the current month and at the last step we look for the month name inside our previously defined array.

The example result of this program is:

Month name: September

The better way to get the month names or week names is to use the java.text.DateFormatSymbols class. The example on using this class can be found on the following links: How do I get a list of month names? and How do I get a list of weekday names?.

How do I get date, month, year values from the current date?

What date, month, year, day-of-week, day-of-month, day-of-year is it today? If we want to answer these question we can use java.util.Calendar and java.util.GregorianCalendar which is the implementation of Calendar abstract class.

These classes can help us to get integer value such as date, month, year from a Date object. Let’s see the example code.

package org.kodejava.util;

import java.util.Calendar;

public class CalendarExample {
    public static void main(String[] args) {
        // Get various information from the Date object.
        Calendar cal = Calendar.getInstance();
        int day = cal.get(Calendar.DATE);
        int month = cal.get(Calendar.MONTH) + 1;
        int year = cal.get(Calendar.YEAR);
        int dow = cal.get(Calendar.DAY_OF_WEEK);
        int dom = cal.get(Calendar.DAY_OF_MONTH);
        int doy = cal.get(Calendar.DAY_OF_YEAR);

        System.out.println("Current Date: " + cal.getTime());
        System.out.println("Day         : " + day);
        System.out.println("Month       : " + month);
        System.out.println("Year        : " + year);
        System.out.println("Day of Week : " + dow);
        System.out.println("Day of Month: " + dom);
        System.out.println("Day of Year : " + doy);
    }
}

Here is the result of this example:

Current Date: Wed Sep 15 17:34:44 CST 2021
Day         : 15
Month       : 9
Year        : 2021
Day of Week : 4
Day of Month: 15
Day of Year : 258

You might also want to try to use the Joda Time library for this. Here another example for getting information about date and time using Joda: How do I get date / time fields of date in Joda?.