java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to get day of week?

In this example we want to create a calendar / date from year and day of the year, because that's all we have. Next we will find what is the day of week of the calendar object is. Let's see the example below.


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, 2007);
        calendar.set(Calendar.DAY_OF_YEAR, 180);
        // See the full information of the calendar object.
        // Get the weekday and print it
        int weekday = calendar.get(Calendar.DAY_OF_WEEK);
        System.out.println("Weekday: " + weekday);

Below is the result of our program.

Fri Jun 29 17:16:22 ICT 2007
Weekday: 6