Algorithm for calculating the day name for a date

For example, I have a date 10.5.2010 and want to know what the day is called (Monday, Tuesday, ...).

Now I need an algorithm that has parameters int year

, int month

, int day

and calculates the name of the day, which was on 10.05.2010. BUT the algorithm must also be able to calculate the days that are in the future.

I have hardcoded into my code that 9/10/2014 is Wednesday, so I can calculate using that. Also you have to be careful with leap years because you got another day.

Can someone help me, just pseudo code if possible, I want to try and do the biggest part myself. If there are multiple lines of code, everything is fine, but please not the complete executable code

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

).

+3


source to share


5 answers


Here's some pseudocode for you, of course not tested, 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 have gone through these ifs to determine the number of days less / more than the current date, the module will get the result from 7 (x% 7) to get the number of days forward / backward, example



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

if it is for 9 days, 9% 7 = 2, so Wednesday - 2 = Monday

0


source


Use an existing algorithm / formula to get the day of the week from a specific date. There is a formula derived from the Gauss formula that describes the method for calculating the day of the week during January 1st in any given year. The resulting formula is 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 equal m-2 mod 12

, January will be year-1(shifting to previous year since month is getting negative)

also a month-2 (i.e 12-2 = 10 , 12 is because year is shifted to previous)

month, similarly February will be year-1

and month 11 (12-1=11)

, march will be in the same year and month3-2 =1

d = days of the month,

w = d + [2.6 m - 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, etc.



Example to understand: -

On January 1, 2000, the date will be considered 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 having to hard-code a specific day and calculate the days based on that.

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

+3


source


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

It is relatively easy to implement and set 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 by an integer from 0 to 6, where 0 is Sunday and 6 is Saturday. Match an integer to a name if you need a string representation of the day.

The following example is written in PHP, not Java, but you can 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

+1


source


To find the weak days.

      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);  

      

In the future, you can add the date to the current date, and after adding in the same way, you can get a weak day.

    Calendar cal = GregorianCalendar.getInstance();
        cal.setTime(start_date);
        cal.add(Calendar.DATE, days_in_futur)));
        java.util.Date next_date = cal.getTime();

      

0


source


My solution in the past has been 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 day names.

The same trick, using the length of the month in days (which is not an integer) and a known reference, can be used to set the approximate phase of the moon for a given date. A better approximation would be to take into account the time of day, and I'm sure astronomers will want to influence the changes in both the length of the day and the length of the lunar month over time ... but that was close enough for my purposes.

0


source







All Articles