How do I convert day-of-the-year to date?

In the following example we want to get the date of the specified day-of-the-year. We can define a calendar for a specific day of the year by setting the java.util.Calendar object DAY_OF_YEAR field using the set() method. The method take the field to be set and a value.

package org.kodejava.example.util;

import java.util.Calendar;

public class DayOfYearToDate {
    public static void main(String[] args) {
        // In the example we want to get the date value of the specified
        // day of the year. Using the calendar object we can define our
        // calendar for a specific day of the year.
        int dayOfYear = 112;
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, dayOfYear);
        System.out.println("Day " + dayOfYear + " of the current year = "
                + calendar.getTime());

        // If you want to get the date for a specific day of year and for
        // a specific year, you can also pass the year information to the
        // calendar object.
        int year = 2020;
        calendar.set(Calendar.YEAR, year);
        System.out.println("Day " + dayOfYear + " in year " + year
                + " = " + calendar.getTime());
    }
}

And here is an example result of the code above:

Day 112 of the current year = Sat Apr 22 00:34:10 WITA 2017
Day 112 in year 2020 = Wed Apr 22 00:34:10 WITA 2020

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

package org.kodejava.example.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, 2017);
        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: Thu Jun 29 19:50:34 WITA 2017
Calendar day of month: 29
Calendar day of week: 5

How do I get day of week?

In this example we want to create a calendar or date from 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 2017.

Let’s see the example below.

package org.kodejava.example.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, 2017);
        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.

Thu Jun 29 13:09:59 WITA 2017
Weekday: 5
Weekday: Thursday