2

I've the date 10.5.2010 for example and want to know what's the name of the day (Monday, Tuesday, ...).

Now I need an algorithm which has the parameters int year, int month, int day and calculates the name of the day which was at the 10.5.2010. BUT the algorithm also must be able to calculate days, which are in the future.

I hardcoded into my code, that the 10.9.2014 is a Wednesday, so I can calculate by using that. Also you must be careful with leap-years because you got one day more to count.

Can someone help me, just pseudo code if it's possible, I want to try to make the biggest part on my own. If there are a few lines of code it's okay, but please no full runnable code

I don't want to use any Java libraries to make this easier (e.g. no Calendar).

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
Basti
  • 1,117
  • 12
  • 32
  • 2
    Why don't you just use http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html ? – Erich Kitzmueller Sep 10 '14 at 06:35
  • 1
    The question is a possible duplicate of http://stackoverflow.com/questions/5270272/how-to-determine-day-of-week-by-passing-specific-date – Codor Sep 10 '14 at 06:35
  • Google for DAY_OF_WEEK constant - returns. – Betlista Sep 10 '14 at 06:36
  • possible duplicate of [How to get localized short day-in-week name (Mo/Tu/We/Th...)](http://stackoverflow.com/questions/3790954/how-to-get-localized-short-day-in-week-name-mo-tu-we-th) – Sezin Karli Sep 10 '14 at 06:37
  • 3
    I don't wanna use already coded things to solve this. I wan't to practice a bit to understand how things are working in java and i think this algorithm could be a good start :) – Basti Sep 10 '14 at 06:40
  • possible duplicate of [Find name of day by year, month and day](http://stackoverflow.com/questions/6789437/find-name-of-day-by-year-month-and-day) – Damian Leszczyński - Vash Sep 10 '14 at 06:41
  • 2
    @Basti If it's a way for you to practice, why don't you try to write something, and ask us if you encounter a problem ? – Florent Bayle Sep 10 '14 at 06:42
  • @Florent Bayle i wrote already a own date class, the only thing which is missed is this algorithm and i had no idea how to do it. i tried to get one by thinking on my own, but i had no clue how i could solve this problem. no i've :) – Basti Sep 10 '14 at 07:26

5 Answers5

2

Use an existing algorithm/formula to derive day of the week from a given date. There are formula's derived from Gauss's formula which described a method for calculating the day of the week for the first of January in any given year. The derived formula's are applicable for all dates.

Let A - 1 = year = Y

m = month - 2 mod 12 (March = 1,..., January = -1 mod 12 = 11 and February = 12)

Since month m is m-2 mod 12 , january would be year-1(shifting to previous year since month is getting negative) and month-2 (i.e 12-2 = 10 , 12 is because year is shifted to previous) month, similarly february would be year-1 and month 11 (12-1=11), march will be same year and month 3-2 =1

d = days of the month,

w = d + [2.6m - 0.2] + 5R(Y,4) + 4R(Y,100) + 6R(Y,400) \mod 7.

Here w is the week of the day, for example 0 for sunday, 1 for saturday and so on.

An example to make it clear:-

For January 1, 2000, the date would be treated as the 11th month of 1999,

d = 1

[2.6 × 11 - 0.2] = 28 mod 7 = 0

5R(99,4) = 5 × 3 = 15 mod 7 = 1

4R(1999,100) = 4 × 99 mod 7 = 4 × 1 = 4

6R(1999,400) = 6 × 399 mod 7 = 6 × 0 = 0

3R(99,7) = 3 × 1 = 3

5R(19,4) = 5 × 3 mod 7 = 1

w = 1 + 0 + 1 + 4 + 0 = 1 + 0 + 1 + 3 + 1 = 6 = Saturday.

You can code this algorithm and it should work like a charm, without need to hardcode a particular day and calculate days based on that.

Note:-This is for Gregorian calendar , for other calendars and for more examples refer here

Community
  • 1
  • 1
Mustafa sabir
  • 4,130
  • 1
  • 19
  • 28
  • I'm going to write this algorithm in Java, without using the Gregorian calendar. But I don't understand what is "R" in the formula meaning? – Snowbases Feb 08 '19 at 12:59
2

You can use the Conway's Doomsday Rule to determine the day of the week of a given date.

It's relatively easy to implement and given three parameters year (YYYY), month (MM) and day (DD) it will produce a number between 0 and 6 representing the day of the week on that date.

The day of the week is represented with an integer between 0 and 6, where 0 is Sunday and 6 Saturday. Map the integer to a name if you need a string representation of the day.

The following example is written in PHP rather than Java, but you should be able to easily translate it:

$months = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4];

$year  -= $month < 3 ? 1 : 0;

return ($year
      + $year / 4
      - $year / 100
      + $year / 400
      + $months[$month - 1]
      + $day) % 7;

Sources

Wikipedia.org
DoomsdayRule.blogspot.com

0

Here's some pseudocode for you, haven't tested it of course, but something like this should work:

int day, month, year;
int baseDay = 10, baseMonth = 9, baseYear = 2014;

int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}

day = getDayFromDate();
month = getMonthFromDate();
year = getYearFromDate();

if year > baseYear
    if month > baseMonth
        if day > baseDay
            //
        else
            //
    else
        if day > baseDay
            //
        else
            //
else
    if month > baseMonth
        if day > baseDay
            //
        else
            //
    else
        if day > baseDay
            //
        else
            //

Once you've been through those ifs to determine the number of days less / greater than the current date, modulus the result with 7 ( x % 7) to get how many days ahead / behind, example

if it's 38 days ahead, 38 % 7 = 3, so that's wednesday + 3 = Saturday

if it's 9 days behind, 9 % 7 = 2, so that's Wednesday - 2 = Monday

Shadow
  • 3,926
  • 5
  • 20
  • 41
0

For finding days of weak .

      String input_date="10/09/2012";
      SimpleDateFormat format1=new SimpleDateFormat("dd/MM/yyyy");
      java.util.Date dt1=format1.parse(input_date);
      DateFormat format2=new SimpleDateFormat("EEEE"); 
      String finalDay=format2.format(dt1);  

for future you can add date to current date, and after adding in similar way you can get day of the weak.

    Calendar cal = GregorianCalendar.getInstance();
        cal.setTime(start_date);
        cal.add(Calendar.DATE, days_in_futur)));
        java.util.Date next_date = cal.getTime();
Tasawer Nawaz
  • 927
  • 8
  • 19
0

My solution in the past was to calculate the Julian/Astronomical Day Number, take that modulo seven, and use a known date to establish a mapping from those 0-6 values to the day names.

The same trick, using the length of a month in days (which isn't an integer) and a known reference, can be used to establish approximate moon phase on a given date. A better approximation would have to account for time of day, and I'm sure astronomers would want to factor in changes in both length of day and length of lunar month over time... but this was close enough for my purposes.

keshlam
  • 7,931
  • 2
  • 19
  • 33