Home Java Beginners Find Day of Month

 
 

Share on Google+Share on Google+

Find Day of Month

Advertisement
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  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.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 6, 2007

Related Tutorials

Discuss: Find Day of Month  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
Ivon Sumana-Ang
August 17, 2011
coding time

so glad to gst it
Simon Greenaway
May 12, 2012
Better method

int dom=Integer.parseInt(new SimpleDateFormat("MM").format(new Date())); int dow=Integer.parseInt(new SimpleDateFormat("dd").format(new Date()));
Deepak Tiwari
October 4, 2012
to discuss any topic for java

hi sir
DMCA.com