获取正确的DAY_OF_WEEK字符串

时间:2015-05-11 10:14:16

标签: java mysql sql

我遇到的问题是我在查询中总是得到sun天,尽管今天monday也是我的查询mon-fri。在我的代码中,WDay的值为2,因此我构建了这样的switch case结构,所以我今天应该得到值mon-fri而不是sun

Calendar calendar = Calendar.getInstance();
    int WDay = calendar.get(Calendar.DAY_OF_WEEK); //Here day has the value 2.

    // Create a statement
    Statement stt = con.createStatement();

    DatabaseMetaData dbm = con.getMetaData();

    ResultSet stopsExist = dbm.getTables(null, null, "stops", null);

    if (stopsExist.next()) {
        // the stops and arrrivaltimes tables exist.

        PreparedStatement preparedLatLong = con
                .prepareStatement("SELECT lat, longi, name from 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 the query here, day has the string sun instead of mon-fri
                PreparedStatement preparedTime = con
                        .prepareStatement("SELECT route from arrivaltimes INNER JOIN stops"
                                + " ON arrivaltimes.stop_id=stops.stop_id "
                                + "WHERE weekday = '" + day + "'"
                                + " and time_format(arrivaltime,'%H:%i')= time_format(curtime() ,'%H:%i') and name LIKE '" + stop_name + "'");

4 个答案:

答案 0 :(得分:2)

问题出在您的switch声明中。如果您在每个案例后都没有添加break;,那么您的代码将继续执行下一个案例。

这应该解决它:

            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;

            }

答案 1 :(得分:1)

您忘记在每个案例后添加break;

来自docs

  

break语句是必要的,因为没有它们,语句就可以了   切换块通过:匹配大小写后的所有语句   标签按顺序执行,无论表达式如何   后续案例标签,直到遇到中断语句

答案 2 :(得分:1)

您需要在每个块中添加break语句:

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);

    }

我也喜欢总是添加一个默认案例,在这种情况下,它会更不合理,但有时这会帮助你处理一些你从未想过的奇怪案例。

如果知道,您可以使用Java的直通机制,不间断地完成任务。取决于您认为更具可读性的内容。至少这使得更改变得更容易,因为您只需编辑一行而不是五行(甚至更多行)。

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);
    }

答案 3 :(得分:1)

前五个CASE可以简化:

switch (WDay) {
        case 7 :
            day = "sat";
            break;
        case 1 :
            day = "sun";
            break;
        default :
            day = "mon-fri";
    }