Calendar.DAY_OF_WEEK dosn工作正常 - ANDROID

时间:2017-07-29 10:10:06

标签: java android calendar dayofweek

我正在开发一个应用程序,它需要获取当前星期和当前时间来根据日期和日期更改整数。

我已经做到了这一点,但我遇到了一些问题: 它在周二中午冻结。 它给了我这个::

Unexpected constant; 
expected one of: Calendar.FRIDAY, Calendar.MONDAY, Calendar.SATURDAY, Calendar.SUNDAY, Calendar.THURSDAY, Calendar.TUESDAY, Calendar.WEDNESDAY less...
This check warns if a switch statement does not explicitly include all the 
values declared by the typedef @IntDef declaration.

而且我不知道如何解决它。

有我的代码: 当我写这篇文章时,除星期二外它有效:

int countuser = 0;
                int day = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
                int timeOfDay = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);

                switch (day) {
                    case Calendar.FRIDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 0;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 1;
                        }
                        break;
                    case Calendar.MONDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 2;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 3;
                        }
                        break;
                    case Calendar.SATURDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 4;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 5;
                        }
                        break;
                    case Calendar.SUNDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 6;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 7;
                        }
                        break;
                    case Calendar.THURSDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 8;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 9;
                        }
                        break;
                    case Calendar.TUESDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 10;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 11;
                        }
                        break;
                    case Calendar.WEDNESDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 12;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 13;
                        }
                        break;
                }

但是我想根据当天对countuser进行排序,所以我做了这个,但它冻结了我的应用程序:

switch (day) {
                    case Calendar.FRIDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 8;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 9;
                        }
                        break;
                    case Calendar.MONDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 0;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 1;
                        }
                        break;
                    case Calendar.SATURDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 10;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 11;
                        }
                        break;
                    case Calendar.SUNDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 12;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 13;
                        }
                        break;
                    case Calendar.THURSDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 6;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 7;
                        }
                        break;
                    case Calendar.TUESDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 2;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 3;
                        }
                        break;
                    case Calendar.WEDNESDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 4;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 5;
                        }
                        break;
                }

同样的,当我尝试排序&#34;案例Calendar.MONDAY&#34;:

 switch (day) {
                    case Calendar.MONDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 0;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 1;
                        }
                        break;
                    case Calendar.TUESDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 2;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 3;
                        }
                        break;
                    case Calendar.WEDNESDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 4;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 5;
                        }
                        break;

                    case Calendar.THURSDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 6;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 7;
                        }
                        break;
                    case Calendar.FRIDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 8;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 9;
                        }
                        break;

                    case Calendar.SATURDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 10;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 11;
                        }
                        break;
                    case Calendar.SUNDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 12;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 13;
                        }


                }

那么我该如何才能获得当前一周的时间来更改整数的值?我尝试了很多东西,但要么冻结,要么它整天都不起作用......

非常感谢。

修改

以下是我的导入列表:

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.support.v7.app.AlertDialog;
import android.text.Html;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

1 个答案:

答案 0 :(得分:0)

我没有足够的回复评论,但你可以发布你的进口吗?并尝试将其放入try-catch块中。

编辑:

try-catch块将尝试 捕获您的代码可能抛出的异常。

    try {
        switch (day) {
                    case Calendar.FRIDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 0;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 1;
                        }
                        break;
                    case Calendar.MONDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 2;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 3;
                        }
                        break;
                    case Calendar.SATURDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 4;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 5;
                        }
                        break;
                    case Calendar.SUNDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 6;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 7;
                        }
                        break;
                    case Calendar.THURSDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 8;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 9;
                        }
                        break;
                    case Calendar.TUESDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 10;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 11;
                        }
                        break;
                    case Calendar.WEDNESDAY:
                        if (timeOfDay > 22 || timeOfDay < 16) {
                            countuser = 12;
                        } else if (timeOfDay >= 16 || timeOfDay <= 22) {
                            countuser = 13;
                        }
                        break;
        }
    } catch(Exception e){
        e.printStackTrace();
    }