# Calander class - calc the num of months weeks and days

Peter Primrose
Ranch Hand
Posts: 755
Hi there,

since i'm not familiar with the Calander class, I wonder if someone can help me out here.

Say today is 01-Jun-05 and X (variable) is 237 days from today.
how can I calculate how many months, weeks and days are there from 01-jun-05

thanks
peter

Layne Lund
Ranch Hand
Posts: 3061
Originally posted by Peter Primrose:
Hi there,

since i'm not familiar with the Calander class, I wonder if someone can help me out here.

One way to become familiar with a class is to look at the API docuemntation for the version of Java you are using. These docs give all the classes and methods in the Standard API. For example, you can look at the docs for the Calendar class to see all the methods it contains. I strongly suggest you learn how to navigate the API docs to be able to easily look up information or learn about unfamiliar classes.

Say today is 01-Jun-05 and X (variable) is 237 days from today.
how can I calculate how many months, weeks and days are there from 01-jun-05

thanks
peter

I don't know if the Calendar class will be helpful here. I suggest that you step back and figure out how to do this by hand. What steps would you use to calculate the number of months, weeks, and days that gives you the date that is 237 days from 01-Jun-05? If you post some of your ideas here, we will be happy to guide you and help you if you get stuck.

Layne
[ July 30, 2005: Message edited by: Layne Lund ]

Peter Primrose
Ranch Hand
Posts: 755
Well, I managed to get the time diff bewteen 2 dates in miliseconds

long difference = now.getTimeInMillis() - old.getTimeInMillis();

now (easy) I can calc the num of days but what I can't figure it out how to get the num of year/months/weeks (feb=28days, aug=31...)

Abdulla Mamuwala
Ranch Hand
Posts: 225
To calculate the number of weeks/months/years from milliseconds, you can follow this approach :

Ask yourself how many milliseconds make up a second, how amny seconds make up a minute, how many minutes make up hour, how many hours make up a day, how many days make up a week.

Now use the expression, milliseconds/week to get the number of weeks.
Same for month and year.

Dror Guzman
Greenhorn
Posts: 5
>Now use the expression, milliseconds/week to get the number of weeks.
Same for month and year.

this holds true for days and weeks but not for months!! (or a leap year)

consider this:
Q: How many sec are there in one minute?
A: 60 - ALWAYS! (well...unless you are not on planet earth :-) )

How many weeks are there in one month?
Depends!!! - is it February or August (and Peter pointed out this correctly)

The bottom line is that there are no 4 weeks in a month and the calc offered to derived from the millisocnds will not help you in this case. you can use it for days and weeks (year also but check special cases where the year can be divided by 4 or 400 - leap year)

Abdulla Mamuwala
Ranch Hand
Posts: 225
Thanks Drur for pointing that out, I had not thought in terms of having different number of days in different months. Also I did not consider the leap year factor.
[ July 31, 2005: Message edited by: Abdulla Mamuwala ]

Layne Lund
Ranch Hand
Posts: 3061
Originally posted by Peter Primrose:
Well, I managed to get the time diff bewteen 2 dates in miliseconds

long difference = now.getTimeInMillis() - old.getTimeInMillis();

now (easy) I can calc the num of days but what I can't figure it out how to get the num of year/months/weeks (feb=28days, aug=31...)

Once again, I suggest that you TURN OFF YOUR COMPUTER and figure out how to do this by hand. What steps would need to do if all you have are a pencil and paper (and maybe a four function calculator to do the arithmetic for you)? So for example, let's say someone told you to do this by hand for 1 million milliseconds. What steps do you take to calculate how many days, months, and years that this is equivalent to? Unless you know how to do this by hand, it will be very difficult to make the computer do it for you.

Once you figure out the steps it takes, feel free to post your ideas here. We will gladly give some feedback and help you from there.

Regards,

Layne