File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Need to find day of the week Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need to find day of the week" Watch "Need to find day of the week" New topic
Author

Need to find day of the week

bob morkos
Ranch Hand

Joined: Sep 06, 2001
Posts: 104
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

Joined: May 22, 2003
Posts: 657



Formatting a Date Using a Custom Format
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

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

Joined: Sep 06, 2001
Posts: 104
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

Joined: May 22, 2003
Posts: 657

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.
 
 
subject: Need to find day of the week