Find Day of Month

This example explores how to find the day of a month and the day of a week

Find Day of Month

This example explores how to find the day of a month and the day of a week

Find Day of Month

Find Day of Month

     

This example explores how to find the day of a month and the day of a week  This example sets the year as 2007 and the day as 181. The example  finds the day of a month and the day of a week by using get(field_name) method.

The Calendar class extends Object class. It is an abstract base class and converts a Date object into a set of integer fields. Calendar class provides a getInstance()  method that returns a Calendar object whose time fields are initialized with the current date and time.

The methods used:
getTime():
This method is used to get current time from calendar.
getInstance():
This method gets a calendar using the default time zone , locale and current time.

The fields used:
WEEK_OF_MONTH:
This field is used to get and set the week indicating the week number within the current month.
DAY_OF_WEEK:
This field gets and set the day indicating the day of the week. This field takes values SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY.

The code of the program is given below:

import java.util.Calendar;
 
public class DayYearToDayMonth 
{
  public static void main(String[] args)
  
  Calendar calendar = Calendar.getInstance();
  calendar.set(Calendar.YEAR, 2007);
  calendar.set(Calendar.DAY_OF_YEAR, 181);
  System.out.println("\nThe date of Calendar is: " +
 calendar.getTime
());
  int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);  
  System.out.println("The day of month: " +
 dayOfMonth
);  
  int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
  System.out.println("The day of week: " 
dayOfWeek
);
  }
}

The output of the program is given below:

C:\rajesh\kodejava>javac DayYearToDayMonth.java
C:\rajesh\kodejava>java DayYearToDayMonth
The date of Calendar is: Sat Jun 30 17:03:01 GMT+05:30 2007
The day of month: 30
The day of week: 7

Download this example.