Home > Java > Calculate day of week

Calculate day of week

Below is a small java program used to calculate which day in week a date is based on the input date. This function is implemented without using any function of Date library.

public class Cal {
public static void main (String args[]) {
int y = 2009;
int m = 9;
int d = 20;

int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};

y -= ((m < 3) ? 1 : 0);

System.out.println("Result:" +(y + y/4 - y/100 + y/400 + t[m-1] + d) % 7);
}
}

The output result’ll be a number from [0] to [6] that is corresponding to [Sunday] ~ [Saturday] in a week

Advertisements
Categories: Java Tags:
  1. No comments yet.
  1. September 24, 2009 at 10:58 AM
  2. January 21, 2010 at 8:11 PM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: