user2923066
user2923066

Reputation:

In which year was the date the same as the original year?

It's my first question on this site, but I always found this site really useful.
What I mean with my question is:

  1. you ask the person to give a date (eg. Fill in a date [dd-mm-yyyy]: 16-10-2013)
  2. you than have to ask an interval between 2 years (eg. Give an interval [yyyy-yyyy]:1800-2000)

When the program runs, it has to show what day of the week the given date is. In this case it was a Wednesday. Than the program has to look in which year, in between the interval, the date 16 October also fell on a Wednesday.

So in the end it has to look something like this:

Fill in a date: [dd-mm-yyyy]: 16-10-2013
Give an interval [yyyy-yyyy]: 1900-2000
16 October was a wednesday in the following years:
1905 1911 1916 1922 1933 1939 1944 1950 1961 1967 1972 1978 1989 1995 2000

The full date is Wednesday 16 October, 2013

The small (or biggest) problem is, I am not allowed to use the DATE.function in java.

If someone can help me with the second part I would be really really happy, cause I have no idea how I am supposed to do this

To find out what day of the week the given date falls, I use the Zeller Congruence


class Day {

Date date; //To grab the month and year form the Date class
           //In this class I check whether the giving date is in the correct form
int day;
int year1; //First interval number
int year2; //Second interval number

final static String[] DAYS_OF_WEEK = {
        "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
        "Friday"
    };

public void dayWeekInterval{
    int interval.year1;
    int interval.year2;

    for(int i = year1; year1 =< year2; year1++) {
        //check if the day of the week in the giving year, is the same as the
        //original year.
    }
}

public void dayOfTheWeek {
    int m = date.getMonth();
    int y = date.getYear();

    if (m &lt; 3) {
        m += 12;
        y -= 1;
    }

    int k = y % 100;
    int j = y / 100;

    int day = ((q + (((m + 1) * 26) / 10) + k + (k / 4) + (j / 4)) +
        (5 * j)) % 7;

    return day;
}

public string ToString(){
    return "" + DAYS_OF_WEEK[day] + day;
}


Hey, I changed my code a bit, but I don't know how to knot the tie. Also I forgot to mention, I am not allowed to use the Date and Calendar function of java... And I pretty much did something wrong with the outlook..

Upvotes: 2

Views: 178

Answers (2)

Deepak Bhatia
Deepak Bhatia

Reputation: 6276

Just a simple formula to find day for given date dd - MM - yyxx is,

( dd + m + xx + (xx/4) + (yy%4) ) % 7
 % is modulus operator which is remainder in general

The answer got above will tell you day of week i.e. 0 : Mon 1: Tue .... 6 for Sun
Here,

dd - Date given
m - month value which is shown down in list calculated with MM value yy - first two digits of supplied year
xx - last two digits of year

Now, m value calculation is,

  • 0 for Jan and Oct
  • 1 for May
  • 2 for August
  • 3 for Feb, March and Nov
  • 4 for June
  • 5 for Sept and Dec
  • 6 for July and April

Remember if month supplied is Jan or Feb and the year supplied is leap then subtract 1 from m value in above table i.e. -1 for Jan and 2 for Feb
Leap Year Calculation is

if (yyyy % 4 == 0)
{
   if( yyyy % 100 == 0)
   {
      return (yyyy % 400) == 0;
   }
   else
     return true;     
}

I hope rest of programming you can do.
This will help you find the day of week for supplied date and now you just need to add loop for all the years.

Upvotes: 1

jboi
jboi

Reputation: 11912

You cannot use Datebut can you use Calendar? Then this would be your code:

    Calendar c = Calendar.getInstance();
    c.set(2013, 9, 16); // month starts at zero
    System.out.printf("Original date is: %tc\n", c);

    int weekday = c.get(Calendar.DAY_OF_WEEK);
    System.out.printf("Weekday of original date is [by number] %d\n", weekday);

    for(int year = 1800; year < 2000; year++) {
        c.set(Calendar.YEAR, year);
        if(weekday == c.get(Calendar.DAY_OF_WEEK))
            System.out.printf("%tc was same weekday!\n", c);
    }

Upvotes: 0

Related Questions