• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Mustafa Segun
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
 
Darryl Burke
Bartender
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SimpleDateFormat already has a format code for obtaining the textual day of week, so why are you reinventing the wheel? Or is this homework?
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Understand.
 
Mustafa Segun
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much all .....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic