java - Get the correct DAY_OF_WEEK String -


i facing problem getting day sun in query although today monday query mon-fri. in code wday has value 2 therefor have built switch case structure should today value mon-fri , not sun?

calendar calendar = calendar.getinstance();     int wday = calendar.get(calendar.day_of_week); //here day has value 2.      // create statement     statement stt = con.createstatement();      databasemetadata dbm = con.getmetadata();      resultset stopsexist = dbm.gettables(null, null, "stops", null);      if (stopsexist.next()) {         // stops , arrrivaltimes tables exist.          preparedstatement preparedlatlong = con                 .preparestatement("select lat, longi, name stops");         resultset rslatlong = preparedlatlong.executequery();         while (rslatlong.next()) {             double lat_stop = rslatlong.getdouble("lat");             double lon_stop = rslatlong.getdouble("longi");             double diststops = haversinedistance(latd, longd, lat_stop,                     lon_stop);             if (diststops <= 10) {                 string stop_name = rslatlong.getstring("name");                  string day = "";                 switch (wday) {                 case 2:                     day = "mon-fri";                 case 3:                     day = "mon-fri";                 case 4:                     day = "mon-fri";                 case 5:                     day = "mon-fri";                 case 6:                     day = "mon-fri";                 case 7:                     day = "sat";                 case 1:                     day = "sun";                  }                  //in query here, day has string sun instead of mon-fri                 preparedstatement preparedtime = con                         .preparestatement("select route arrivaltimes inner join stops"                                 + " on arrivaltimes.stop_id=stops.stop_id "                                 + "where weekday = '" + day + "'"                                 + " , time_format(arrivaltime,'%h:%i')= time_format(curtime() ,'%h:%i') , name '" + stop_name + "'"); 

you need add break statements in each block:

switch (wday) {         case 2 :             day = "mon-fri";             break;         case 3 :             day = "mon-fri";             break;         case 4 :             day = "mon-fri";             break;         case 5 :             day = "mon-fri";             break;         case 6 :             day = "mon-fri";             break;         case 7 :             day = "sat";             break;         case 1 :             day = "sun";             break;         default :             throw new runtimeexception("illegal day: " + wday);      } 

i always add default case, in case more out of principle helps in weird cases never thought of.

if aware, without breaks, using java's fall-through mechanics. depends on consider more readable. @ least makes changes easier have edit 1 line instead of 5 (or more lines).

switch (wday) {         case 1 :             day = "sun";             break;         case 2 : // fall-through         case 3 : // fall-through         case 4 : // fall-through         case 5 : // fall-through         case 6 :             day = "mon-fri";             break;         case 7 :             day = "sat";             break;         default :             throw new runtimeexception("illegal day: " + wday);     } 

Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -