How do I get day of week?

Category: java.util, viewed: 1768 time(s).

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.java.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.
        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);
    }
}
 
 

Below is the result of our program.

Fri Jun 29 17:16:22 ICT 2007
Weekday: 6
Bookmark this example!  

Most Viewed Examples

Google

100 Top & Latest


eXTReMe Tracker
visitor stats