How do I add n days to a date in Java Creating my own java class?
For example, my date is (dd/mm/yyyy) = 26/02/2014
Adding 3 days, the output should be 01/03/2014
.
Without importing Calendar or Date from JAVA API
Thank you in Advance. Any Sample code or Pseudo Code or idea will be highly apprecited
Since everyone is putting their Date class here, I'll post mine.
Here are test results.
And here's the code. I formatted my dates in a month, day, year format. You can change the getFormattedOutput method to be in a day, month, year format.
try this
note that code may need fixing
simple code for add or sub using method
Convert the date to days. For example how many days passed from
01/01/1900
then add 3 days and convert it back.This was one of my interview question, I had return the below code on paper may be my bad luck :-( I didn't get selected in that round, may be interviewer was not able to understand :-) :-). I just searched to see is there any better way of doing it but I didn't, so I wrote same code what I had written on paper but its working like charm. Hope fully my confidence remains same :-) :-)
Hope this help you or some body.