How to get all possible years from a given day, day of the week and month?

I want to get a set of years related to a specific date, week of the day and month. There are opportunities that I can get for several years from this template. Suppose the date is 15, the month is August, and the week of the day is Tuesday, how can I find a year that is valid? Is there a formula for this?

+7
date math formula
source share
3 answers

Will this completely solve your problem?

The idea behind this:

  • Start on a specific date (August 15) from year 0
  • if the date is indicated on the specified day of the week → add it to your list
  • add 1 year
  • back to square 1

(until reaching specified MAX_DATE)

List<Integer> validYears = new ArrayList<>(); LocalDate date = LocalDate.of(0, Month.AUGUST, 15); while (date.getYear() < 2015) { if (date.getDayOfWeek() == DayOfWeek.TUESDAY) { validYears.add(date.getYear()); } date = date.plusYears(1); } validYears.forEach(year -> System.out.println(year)); 

Edit: if your date is February 29, you must add the following code (because if you add 1 year before 0000-02-29, it will give you 0001-02-28, and from there you will stay on the 28th).

 if (date.isLeapYear()) { //And date was feb. 29 date = date.withDayOfMonth(29); } 

Here is the streaming version:

 int day = 29; Month month = Month.FEBRUARY; List<LocalDate> collect = IntStream.range(0, 2016).filter(year -> day <= LocalDate.of(year, month, 1).lengthOfMonth()) .mapToObj(year -> LocalDate.of(year, month, day)).filter(date -> date.getDayOfWeek() == DayOfWeek.TUESDAY) .collect(Collectors.toList()); collect.forEach((year -> System.out.println(year))); 
+3
source share

Here is a simple formula that you can make even in your head. You can find more information here: How to determine the day of the week, given the month, day and year

  • Take the last two digits of the year.
  • Divide by 4, discarding any part.
  • Add the day of the month.
  • Add Month Key Value: JFM AMJ JAS OND 144 025 036 146
  • Subtract 1 for January or February of a leap year.
  • For a Gregorian date, add 0 for the 1900s, 6 for the 2000s, 4 for the 1700s, 2 for the 1800s; for other years, add or subtract a multiple of 400.
  • For a Julian date, add 1 for 1700 and 1 for every subsequent centuries ago.
  • Add the last two digits of the year.
  • Divide by 7 and take the remainder.
+3
source share

This question relates to the classic Dommsday algorithm.
A simple calculation method is the Kraichik method.

Kraichik Method -
w = d + m + c + y mod 7,
where w is the day of the week (counting up from 1 on Sunday instead of 0 in the Gauss version),
and d, m, c, and y are numbers depending on the day, month, and year, as in the following tables:

 Month 1 2 3 4 5 6 7 8 9 10 11 12 m 1 4 3 6 1 4 6 2 5 0 3 5 

For the Gregorian calendar, take the centenary of the year (for example, in 1986 - 1900, 2014 - 2000). [Century / 100] mod 4: 0 1 2 3
c: 0 5 3 1

For the Julian calendar,
[Century / 100] mod 7: 0 1 2 3 4 5 6
c: 5 4 3 2 1 0 6

Finally, the year number is obtained from this table (with the subtraction of 1 from the dates in January or February):
The last two digits of year y
00 06 17 23 28 34 45 51 56 62 73 79 84 90 0 01 07 12 18 29 35 40 46 57 63 68 74 85 91 96 1 02 13 19 24 30 41 47 52 58 69 75 80 86 97 2 03 08 14 25 31 36 42 53 59 64 70 81 87 92 92 98 3 09 15 20 26 37 43 48 54 65 71 76 82 93 99 4 04 10 21 27 32 38 49 55 60 66 77 83 88 94 5 05 11 16 22 33 39 44 50 61 67 72 78 89 95 6

What is it. You can get more details about the details from wikipedia

+2
source share

All Articles