• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. Understand.
 
Mustafa Segun
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much all .....
 
Create symphonies in seed and soil. For this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic