• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need to find day of the week

 
bob morkos
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • 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);
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Formatting a Date Using a Custom Format
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic