Actually I already knew about Calendar but I couldn't get the right results. That's why I was looking for the algorithm rajatC posted. The funny thing is that yesterday Calendar wasn't giving me the right results but today for some strange reason the code that I wrote worked. That's why I try to avoid Calendar and work only with Date and SimpleDateFormat. I find it simpler.