inconsistant results for Calendar.DAY_OF_WEEK?

Today is monday and it should print 1..

but only second statement is printing one... why?

System.out.println("is today Monday?-->"+Calendar.DAY_OF_WEEK);

System.out.println("This has been depricated with Calendar.DAY_OF_WEEK:" +Calendar.getInstance().getTime().getDay());

[349 byte] By [harry_singha] at [2007-11-26 18:10:56]
# 1

Calendar.DAY_OF_WEEK doesn't give you the day of the week. the fact it worked today is coincidence. it's a constant used to access fields of the calendar. use it like this

Calendar cal = Calendar.getInstance();

int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

Calendar.get is a general-purpose method that returns different fields depending on what you pass in. by massive coincidence, DAY_OF_WEEK is '1' and you first tried this on a Monday :-)

georgemca at 2007-7-9 5:43:26 > top of Java-index,Java Essentials,Java Programming...
# 2
got it...thank u very much.
harry_singha at 2007-7-9 5:43:26 > top of Java-index,Java Essentials,Java Programming...
# 3
no problem!
georgemca at 2007-7-9 5:43:26 > top of Java-index,Java Essentials,Java Programming...
# 4

> System.out.println("is today

> Monday?-->"+Calendar.DAY_OF_WEEK);

I find it a bit odd that you didn't read the javadoc when you found out that it didn't work as expected (well, it's even a bit odd that you didn't read it before you even tried to write the code)

Kaj

kajbja at 2007-7-9 5:43:26 > top of Java-index,Java Essentials,Java Programming...
# 5

dude .. ofcouse i did read that... however, no where it's clear that you have to pass an int to get method and get the day index...

if it clearly says that Calendar.DAY_OF_WEEK should give you day of the week. should be simple as that..but truely does not give you the day of the week....rather gives you number of days in a week.

harry_singha at 2007-7-9 5:43:26 > top of Java-index,Java Essentials,Java Programming...
# 6
i also understand that this was the simplest thing you can ask on this forum...but my freind, some times unclear simple things are harder to undstand than they apear...
harry_singha at 2007-7-9 5:43:26 > top of Java-index,Java Essentials,Java Programming...
# 7

> dude .. ofcouse i did read that... however, no where

> it's clear that you have to pass an int to get method

> and get the day index...

>

> if it clearly says that Calendar.DAY_OF_WEEK should

> give you day of the week. should be simple as

> that..but truely does not give you the day of the

> week....rather gives you number of days in a week.

apart from where it's clearly marked in Teh Docs ™ with Field number for get and set indicating the day of the week, of course. also, didn't you wonder how the Calendar class itself would know which Calendar instance you were interested in? well, it's resolved now so no matter.....

georgemca at 2007-7-9 5:43:26 > top of Java-index,Java Essentials,Java Programming...
# 8

> i also understand that this was the simplest thing

> you can ask on this forum...but my freind, some times

> unclear simple things are harder to undstand than

> they apear...

I can't judge if a question is easy or not, that's up to you. You found it worth to ask so you clearly had a problem.

I thought that the documentation was clear since it says:

DAY_OF_WEEK

Field number for get and set indicating the day of the week.

Kaj

kajbja at 2007-7-9 5:43:26 > top of Java-index,Java Essentials,Java Programming...