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,

[129 byte] By [sunish_josea] at [2007-11-27 10:14:16]
# 1

try the calendar class:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Calendar.html

deAppela at 2007-7-28 15:32:17 > top of Java-index,Java Essentials,Java Programming...
# 2

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.

masijade.a at 2007-7-28 15:32:17 > top of Java-index,Java Essentials,Java Programming...
# 3

Anyone got some sample code?

sunish_josea at 2007-7-28 15:32:17 > top of Java-index,Java Essentials,Java Programming...
# 4

> 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.

masijade.a at 2007-7-28 15:32:17 > top of Java-index,Java Essentials,Java Programming...
# 5

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

malcolmmca at 2007-7-28 15:32:17 > top of Java-index,Java Essentials,Java Programming...
# 6

> 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.

masijade.a at 2007-7-28 15:32:17 > top of Java-index,Java Essentials,Java Programming...
# 7

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.

malcolmmca at 2007-7-28 15:32:17 > top of Java-index,Java Essentials,Java Programming...