[Poll] Date class and method for getting Date after X months. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

[Poll] Date class and method for getting Date after X months.

I'm writing a Date class in Java and I want it to have a method with which you can take an other Date object that corresponds to the date after a number of months. For example "Date dateB = dateA.dateAfterMonths(10);" will result in dateB having the date ten months after dateA's date. The problem is that months don't have the same number of days (as we all know). So, if the date is January 13 and I ask "What the date will be one month from now?" the most expected answer will probably be February 13, but if it is January 31 (or 30, or 29) and I ask the same question what answer would you expect? February 28 (the last day of the next month) or perhaps March 2 (30 days later). The first option gives a more intuitive answer, but lacks consistency. The second option is consistent, but if we assume that each month has on average 30 days then we lose 5 days every year (30 x 12 = 360), and the expression "Date dateB = dateA.dateAfterMonths(60);" will be off for over a whole month. What do you think? TL;DR: The date is January 31 and you are asked "What the date will be, one month from now?", would you answer February 28 (the last day of the next month) or March 2 (30 days later). You can find my Date class here: https://code.sololearn.com/c3LWL1AsWKM0/#java

21st Apr 2018, 8:17 PM
Tanasis
Tanasis - avatar
5 Answers
+ 8
The Java time package answers this question: last day of a month + 1 month = last day of the next month (if the next month has less days). Quite sure it worked like this in the old date package before Java 8 as well.
21st Apr 2018, 9:16 PM
Tashi N
Tashi N - avatar
+ 8
The Java time package answers this question: last day of a month + 1 month = last day of the next month (if the next month has equal or less days). Quite sure it worked like this in the old date package before Java 8 as well.
21st Apr 2018, 9:17 PM
Tashi N
Tashi N - avatar
+ 4
Program your human logic into your code. Use some if statements to check the month and the day of the month and adjust accordingly. For instance, if the date is less than 31 in any month after Feb (Actually Jan lol) you can safely jump to that day up to the 30th of the next month, and in all instances after Feb of it being the 31st of the month you can safely jump to the 1st of the month after next (Mar 31st + 1 Month = May 1st, etc). So the real issues are in Jan and Feb (don't forget leap years). BTW you have an off by 1 error in your logic. Feb 28th + 1 month is Mar 3rd not the 2nd. Be sure not to include the current day in your counting. Here is a calculator example of how your results would typically be; http://www.calculator.net/date-calculator.html
21st Apr 2018, 8:51 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
you can create an array containing the number of days of any month, thus you'll can access this array easily 😊 maybe you need to consider the leap years
24th Apr 2018, 2:30 AM
Marx Guthemberg Veloso
Marx Guthemberg Veloso - avatar
0
that's the problem when u want to create a program basses on real life(in real life we r not that specific and accurate..... ). I say that "a month " isn't a 30 day package..... it's a month! (based on the specific month the number changes)
21st Apr 2018, 8:37 PM
Farshaad Heydari
Farshaad Heydari - avatar