Getting the fist day of a given year.
Do any of u know a program that gets first sunday of a given year?
for 2007 it is January 7
Thanks in advance,
Do any of u know a program that gets first sunday of a given year?
for 2007 it is January 7
Thanks in advance,
Class GregorianCalendar
Method set(int field, int value)
Method get(int field)
Fields YEAR MONTH DAY_OF_MONTH DAY_OF_WEEK SUNDAY JANUARY
A while loop with != SUNDAY
Read the API and use your imagination.
> Anyone got some sample code?
What more do you need? I provided you with every last piece of info you need.
But, if you so hot for a finished example (and this is not hard) Google for some, there are plenty of examples on how to use the varying Calendar classes. With just a little imagination, and the information from my last post, you should be able to get it. You could, at least, make an attempt.
With Calendar you can do it without looping - you can ask for the first Monday of Janurary.
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(Calendar.YEAR, thisYear);
cal.set(Calendar.MONTH, Calendar.JANURARY);
cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
Data firstMonday = cal.getTime();
(You can also ask for the last Monday in the month).
Message was edited by:
malcolmmc
> With Calendar you can do it without looping - you can
> ask for the first Monday of Janurary.
> > Calendar cal = Calendar.getInstance();
> cal.clear();
> cal.set(Calendar.YEAR, thisYear);
> cal.set(Calendar.MONTH, Calendar.JANURARY);
> cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1);
> cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
> Data firstMonday = cal.getTime();
>
>
> (You can also ask for the last Monday in the month).
>
> Message was edited by:
> malcolmmc
Are you sure that this will actually change the date? According to the documentation "the value is not interpreted". I take this to mean that, in this case, it will leave the actual date alone, but set the day of the week to whatever you set it to.
Edit: Okay, reading the long intro to Calendar again, it seems to suggest that it will change the date. Haven't tried it, but cool if it does.
Second Edit: I still, however, do agree with simply giving him the code, though. This is guaranteed to be a homework assignment.
Calender defers calculation of an actual date during a series of set calls. It will calculate the date when there's a getTime() or add() call.