gpt4 book ai didi

java - 获取正确的 DAY_OF_WEEK 字符串

转载 作者:行者123 更新时间:2023-11-29 04:19:20 24 4
gpt4 key购买 nike

我遇到的问题是,尽管今天是星期一,但我的查询mon-fri 也总是得到天sun。在我的代码中,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 + "'");

最佳答案

问题出在您的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;

}

关于java - 获取正确的 DAY_OF_WEEK 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30165015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com