File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes is this not suppose to give me date of the week? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "is this not suppose to give me date of the week?" Watch "is this not suppose to give me date of the week?" New topic
Author

is this not suppose to give me date of the week?

Mustafa Segun
Greenhorn

Joined: Jan 19, 2010
Posts: 18



Please i need correction .. the date this not functioning well in some months, i get Tuesday if today is Monday.
Thanks in addvance
Note: am in Nigeria where our time zone is GMT-8:00hrs


SCJP, SQL EXPERT, OCMJD
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4531
    
    5

SimpleDateFormat already has a format code for obtaining the textual day of week, so why are you reinventing the wheel? Or is this homework?


luck, db
There are no new questions, but there may be new answers.
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
This design could be simplified significantly to:

pseudo:
create a Calendar object
set the Calendar object to the desired date
get the DAY_OF_WEEK of the Calendar object
convert int DAY_OF_WEEK to name of the day
return result

Are you allowed to simplify as described?


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

You're suffering from the same problem as this thread - you use a 1-based month numbering whereas Calendar uses a 0-based month numbering.

But Greg is right. Calendar has a method called setTime which takes a Date object. Also, Calendar.getInstance() already returns a Calendar instance for the current moment, so in this case (where the Date is also the current moment) it's not necessary to call setTime.

One addition to Greg's post:
Greg Brannon wrote:convert int DAY_OF_WEEK to name of the day
return result

DateFormatSymbols can be used for that. The String[] you're interested in is indexed using the return values of Calendar.get so no need for translations there.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
Mr. Spoor,

Thanks, I learned something, but I have a follow up for clarification.

Let's say the Calendar object is today, then today.get( Calendar.DAY_OF_WEEK ) returns an integer, 1 through 7, corresponding to SUNDAY through SATURDAY. My point was, however it's done, the integer returned by Calendar's get() method has to be converted from an integer to the name of the day. As you pointed out, that conversion can be done using DateFormatSymbols.

Is that what you meant, or am I missing something more fundamental?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Nope, that's about it. Please note that DateFormatSymbols supports I18N - different locale means different labels. You'll be able to print the weekday names in English, German, French, etc by just changing the Locale object to use.
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
Thanks. Understand.
Mustafa Segun
Greenhorn

Joined: Jan 19, 2010
Posts: 18
Thanks so much all .....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is this not suppose to give me date of the week?