How do I 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.

package org.kodejava.example.util;

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

Wayan Saryada

Programmer, runner, recreational diver, live in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA.

Leave a Reply