Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners Find the Day of the Week

 
 

Share on Google+Share on Google+

Find the Day of the Week

Advertisement
This example finds the specified date of an year and a day of a week from calendar.

Find the Day of the Week

     

This example finds the specified date of  an year and a day of a week from calendar.

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

The methods used:
getTime(): This method gets the current time from the calendar.

setTimeInMillis(long millis): This method sets the current time in a calendar object.

getInstance(): This method gets a calendar object containing the default time zone, locale and current time.

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

DAY_OF_YEAR: This field gets and sets the day to indicate the day number within a current year. e.g. The first day of the year has value 1.

YEAR: This field is used to get and set the year for indicating the year.

The code of the program is given below:

import java.util.Calendar; 
public class DayOfYearToDayOfWeek{
  public static void main(String[] args){
  Calendar calendar = Calendar.getInstance();
  for(int i=2000;i<2005;i++){
  calendar.set(Calendar.YEAR, i);
  calendar.set(Calendar.DAY_OF_YEAR,0);
  System.out.println(calendar.getTime().toString());
  int weekday = calendar.get(Calendar.DAY_OF_WEEK);
  System.out.println("Weekday: " + weekday);
  System.out.println();
  }
  }
}

The output of the program is given below:

C:\rajesh\kodejava>javac DayOfYearToDayOfWeek
C:\rajesh\kodejava>java DayOfYearToDayOfWeek
Fri Dec 31 16:21:22 GMT+05:30 1999
Weakday: 6
Sun Dec 31 16:21:22 GMT+05:30 2000
Weakday: 1
Mon Dec 31 16:21:55 GMT+05:30 2001
Weekday: 2
Tue Dec 31 16:21:55 GMT+05:30 2002
Weekday: 3
Wed Dec 31 16:21:55 GMT+05:30 2003
Weekday: 4

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 the Day of the Week   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com