S. Martell S. Martell - 5 months ago 14
Android Question

Getting the name of the day before instead of today's with Calendar

Good day. I have a class which should give me the current day name in a String, but instead of giving me the name of the current day, is giving me the name of the day before today.

I've been checking what's going on, on the debugger, and apparently the switch does work on the current day (at the moment, it's Sunday), and puts the correct text and image on my TextView and ImageView. But as soon as the debugger finishes, the image and text shown are the ones meant for Saturday.

Here's my class:

private void checkWeekday() {

String week_day;
Locale loc = new Locale("es","MX");
SimpleDateFormat day_format = new SimpleDateFormat("EEEE", loc);

Calendar calendar = Calendar.getInstance();
week_day = day_format.format(calendar.getTime());
week_day = week_day.toLowerCase();

switch (week_day) {
case "domingo":
textView_weekday_name.setText(sunday);
imageView_diets_weekday.setImageDrawable(drawable_sunday);
case "lunes":
textView_weekday_name.setText(monday);
imageView_diets_weekday.setImageDrawable(drawable_monday);
case "marter":
textView_weekday_name.setText(tuesday);
imageView_diets_weekday.setImageDrawable(drawable_tuesday);
case "miércoles":
textView_weekday_name.setText(wednesday);
imageView_diets_weekday.setImageDrawable(drawable_wednesday);
case "jueves":
textView_weekday_name.setText(thursday);
imageView_diets_weekday.setImageDrawable(drawable_thursday);
case "viernes":
textView_weekday_name.setText(friday);
imageView_diets_weekday.setImageDrawable(drawable_friday);
case "sábado":
textView_weekday_name.setText(saturday);
imageView_diets_weekday.setImageDrawable(drawable_saturday);
}

}


Things to note: This method is being called on a @AfterViews method, inside a Fragment.

I already tried taking the calendar bit to my main @AfterViews method, but it still gets the name of the day before.

I suspect I am trying to get the day name on the wrong place, but no matter where I put it, it will still give me Saturday.

For reference, this is how my @AfterViews method looks:

@AfterViews
protected void init() {

checkWeekday();

boolean logged = Prefs.getBoolean(user_logged, false);
boolean subbed = Prefs.getBoolean(user_subscribed, false);
String email = Prefs.getString(user_mail, "");

mixpanel = MixpanelAPI.getInstance(getContext(), mixpanel_token);

setHasOptionsMenu(true);

if (logged && subbed) {

List<UserMeals> meals = new RushSearch().find(UserMeals.class);

if(meals.size() != 0) {

setDataFromLocal();
onClickHandler();

} else {

try {
checkIfNewPlan(email, "fetch");
} catch (JSONException e) {
e.printStackTrace();
}
onClickHandler();
}
} else {
emptyLayout("nosub");
}

}


Thanks.

EDIT

I was missing break on every case on my switch. So embarrassed about this! Thank you very much!

Answer

To get current date using the Calendar Instance, please use this method:

public static String getCurrentDate(){
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    return (month+1) +"/"+ day + "/" + year;
}

Since the calendar month is 0-based, meaning January is actually 0 instead of 1, you need to add 1 to it!

UPDATE

I just looked at your switch statement and it doesn't have a break in the case statement?

switch (week_day) {
    case "domingo":
        textView_weekday_name.setText(sunday);
        imageView_diets_weekday.setImageDrawable(drawable_sunday);
        break;
    case "lunes":
        textView_weekday_name.setText(monday);
        imageView_diets_weekday.setImageDrawable(drawable_monday);
        break;
    case "marter":
        textView_weekday_name.setText(tuesday);
        imageView_diets_weekday.setImageDrawable(drawable_tuesday);
        break;
    case "miércoles":
        textView_weekday_name.setText(wednesday);
        imageView_diets_weekday.setImageDrawable(drawable_wednesday);
        break;
    case "jueves":
        textView_weekday_name.setText(thursday);
        imageView_diets_weekday.setImageDrawable(drawable_thursday);
        break;
    case "viernes":
        textView_weekday_name.setText(friday);
        imageView_diets_weekday.setImageDrawable(drawable_friday);
        break;
    case "sábado":
        textView_weekday_name.setText(saturday);
        imageView_diets_weekday.setImageDrawable(drawable_saturday);
        break;
}

I have a feeling this is the problem!