-2

I did a lot of research regarding this:

Based on the datepicker in Android Studio, is it possible to do the following calculations?

I want to build an app where you just enter the start date and the number of months and then you will get the end-date based on the calculation. Is it possible that Android gives the correct calculations based on the different number of days during the different months? Can the Android system scan the calender by considering the up-to date calendar?

Thank you :)

Genbu
  • 1
  • 1
  • Have you checked https://developer.android.com/reference/java/util/Calendar.html and read the documentation? The `add` method should do what you need. I I am missing the point, please explain your problem more in detail, – Malte Hartwig May 29 '17 at 20:12
  • 1
    There are plenty of similar questions and answers on Stack Overflow already. Do yourself the favour of searching before posting. For example [How do I add one month to current date in Java?](https://stackoverflow.com/questions/4905416/how-do-i-add-one-month-to-current-date-in-java), – Ole V.V. May 29 '17 at 21:02

1 Answers1

0

Yes, you can. If you are using a calendar you have 3 variable:

  1. Calendar.YEAR
  2. Calendar.MONTH
  3. Calendar.DAY_OF_MONTH

The DAY_OF_MONTH variable won't change, then you just have to do some work with the number of months you get in the input like :

To get the end date year:

(Calendar.MONTH + input_months) % 12

Note : This will give you how many years you have to add to Calendar.YEAR to know the final date.

To get the end date month

int end_date_month = Calendar.MONTH + input_months;
do{
   month -= 12; 
} while (month > 12);

Note : At the end of the do while you will have the end date month, and you are done.

Remember that Calendar.MONTH if the month is January it will return 0, and so on, till December (11).

Coder ACJHP
  • 1,940
  • 1
  • 20
  • 34
  • 1
    FYI, the troublesome old date-time classes such as `java.util.Date`, `java.util.Calendar` and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. Much of the java.time functionality is back-ported to Java 6 & Java 7 in the [ThreeTen-Backport](http://www.threeten.org/threetenbp/) project. Further adapted for Android in the [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) project. – Basil Bourque May 30 '17 at 07:07