Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+14 votes

I'm able to parse a String of date to a Calendar object but then i got stuck on how to subtract those two dates. I tried to change them all in Millis, then change it back to format in yyyy-MM-dd, but it won't work.

asked in CSC490_Spring2019 by (1 point)

3 Answers

+5 votes

I think when you subtract two dates in milis, you would get the time in millis between the two dates. You would then have to convert it back into days and then subtract that number of days from the first date.

Like Dr. Stonedahl mentioned in another question, you might want to try the GregorianCalendar class: https://developer.android.com/reference/java/util/GregorianCalendar

I ended up using the LocalDate class which worked well for me. You can add/subtract days/years using methods like plusDays(), minusYears(), etc: https://beginnersbook.com/2017/10/java-localdate/

answered by (1 point)
+3 votes

You need to use the get day, get month, and get year stuff ( and set). This is done with a generic getField and setField where you have to pass in the DAY or MONTH or YEAR ints found in the Calander class. You can use the getMaxValue and enter month to get the maximum number of days in a month. There is no fast way of doing it that I found. I needed a while loop to deal with days and months, because months suck, but the years were fairly streightforward.

answered by (1 point)
+1 vote

I figured it out like what Jack said i need to get the day month year one by one then add them

answered by (1 point)
...