• 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

Need to find day of the week

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code below returns the wrong day of the week. Could anyone help?

// get the supported ids for GMT-04:00 (Eastern Standard Time)
// when daylight savings time is in effect

// Fours hours behind GMT, zulu, UTC
// SimpleTimeZone est = new SimpleTimeZone(-4 * 60 * 60 * 1000, "EST");

// create a GregorianCalendar with the Eastern Standard
// time zone and the current date and time
Calendar calendar = new GregorianCalendar();
Date dd = new Date(System.currentTimeMillis());
calendar.setTime(dd);

// Cat it all together
String myTime = new String(calendar.get(Calendar.HOUR_OF_DAY) + ":"
+ calendar.get(Calendar.MINUTE));
System.out.println("myTime is:" + myTime);

int dayNum = calendar.get(Calendar.DAY_OF_WEEK);
String day = null;

switch (dayNum) {
case Calendar.SUNDAY:
day = "SUNDAY";
case Calendar.MONDAY:
day = "Monday";
case Calendar.TUESDAY:
day = "Tuesday";
case Calendar.WEDNESDAY:
day = "Wednsday";
case Calendar.THURSDAY:
day = "Thursday";
case Calendar.FRIDAY:
day = "Friday";
case Calendar.SATURDAY:
day = "Saturday";

System.out.println("Day of the week is : " + day);
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Formatting a Date Using a Custom Format
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As to why your code doesn't behave as expected, you've left out the break statements in your switch, so you're always falling all the way down to Calendar.SATURDAY...
 
bob morkos
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Steve, I forgot the break in my code. Anyways, is there a way you can say that the current time is 11:00 am and does it fall between 5:am - 3:00 pm

Originally posted by Steve Morrow:
As to why your code doesn't behave as expected, you've left out the break statements in your switch, so you're always falling all the way down to Calendar.SATURDAY...

 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anyways, is there a way you can say that the current time is 11:00 am and does it fall between 5:am - 3:00 pm

Of course there is! Give it a shot, and let us know if you run into any problems.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic