使用日历获取前一天的名称而不是今天的名称
Getting the name of the day before instead of today's with Calendar
美好的一天。我有一个 class,它应该在一个字符串中给我当前日期的名称,但不是给我当前日期的名称,而是给我前一天的名称。
我一直在调试器上检查发生了什么,显然开关在当天(目前是星期日)确实有效,并将正确的文本和图像放在我的 TextView 和 ImageView 上。但是一旦调试器完成,显示的图像和文本就是星期六的图像和文本。
这是我的 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);
}
}
注意事项:此方法是在 Fragment 内的 @AfterViews 方法上调用的。
我已经尝试将日历位带到我的主要@AfterViews 方法中,但它仍然获取前一天的名称。
我怀疑我试图把日期名称放在错误的地方,但无论我把它放在哪里,它仍然会给我星期六。
作为参考,这是我的@AfterViews 方法的样子:
@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");
}
}
谢谢。
编辑
我在 switch 的每个案例中都缺少 break。太尴尬了!非常感谢!
要使用日历实例获取当前日期,请使用此方法:
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;
}
由于日历月是从 0 开始的,这意味着一月实际上是 0 而不是 1,所以您需要给它加 1!
更新
我刚看了你的 switch 语句,它在 case 语句中没有 break 吗?
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;
}
我感觉这就是问题所在!
美好的一天。我有一个 class,它应该在一个字符串中给我当前日期的名称,但不是给我当前日期的名称,而是给我前一天的名称。
我一直在调试器上检查发生了什么,显然开关在当天(目前是星期日)确实有效,并将正确的文本和图像放在我的 TextView 和 ImageView 上。但是一旦调试器完成,显示的图像和文本就是星期六的图像和文本。
这是我的 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);
}
}
注意事项:此方法是在 Fragment 内的 @AfterViews 方法上调用的。
我已经尝试将日历位带到我的主要@AfterViews 方法中,但它仍然获取前一天的名称。
我怀疑我试图把日期名称放在错误的地方,但无论我把它放在哪里,它仍然会给我星期六。
作为参考,这是我的@AfterViews 方法的样子:
@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");
}
}
谢谢。
编辑
我在 switch 的每个案例中都缺少 break。太尴尬了!非常感谢!
要使用日历实例获取当前日期,请使用此方法:
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;
}
由于日历月是从 0 开始的,这意味着一月实际上是 0 而不是 1,所以您需要给它加 1!
更新
我刚看了你的 switch 语句,它在 case 语句中没有 break 吗?
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;
}
我感觉这就是问题所在!