简体   繁体   中英

Calendar.DAY_OF_WEEK dosn't work fine - ANDROID

i'm developing an Application which need to get the current Day of Week and the current time to change an Integer depending on the day and the date.

I have made this but I have few problems: It freezes on Tuesday midday. It gives me this::

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.

And I don't know how to fix it.

There is my code: When I write this, It works except for Tuesday:

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

But I would like to sort the countuser depending of the day so I made this but it freezes my app:

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

And same for this, when i try to sort the "case Calendar.MONDAY":

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


                }

So how can I do to get the current day of week and time to change the value of my Integer ? I tried a lot of thing but either it freezes or either it doesn't work for all the day...

Thanks a lot.

EDIT:

Here is a list of my import:

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;

I don't have enough rep to comment, but could you post your imports? And try putting this inside an try-catch block.

EDIT:

A try-catch block will try to catch an exception that could be thrown by your code.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM