我试图在我的应用中显示日期名称和日期以及定义为“文字”的文字显示精细和整数值也显示正常,但它不会显示文字变量,这就是我所拥有的:
Calendar now = Calendar.getInstance();
TextView output3 = (TextView) findViewById(R.id.output3);
int dayno = now.get(Calendar.DAY_OF_WEEK);
String dayName = new String();// also tried String dayName = ""; with the same results
if (dayno == 1) dayName = "Sun";
if (dayno == 1) dayName = "Mon";
if (dayno == 1) dayName = "Tue";
if (dayno == 1) dayName = "Wed";
if (dayno == 1) dayName = "Thu";
if (dayno == 1) dayName = "Fri";
if (dayno == 1) dayName = "Sat";
也尝试过这样:
if (dayno == 1) {dayName = "Sun";}
if (dayno == 1) {dayName = "Mon";}
if (dayno == 1) {dayName = "Tue";}
if (dayno == 1) {dayName = "Wed";}
if (dayno == 1) {dayName = "Thu";}
if (dayno == 1) {dayName = "Fri";}
if (dayno == 1) {dayName = "Sat";}
output3.setText(hour + ":" + minute + ":" + second + " Week " + weekno + ", " + dayName + ", " + yearno);
我无法弄清楚这里可能出现什么问题,任何帮助都会受到赞赏
答案 0 :(得分:1)
如果条件允许,您的天数相同。正确撰写您的条件。请改用Switch
。
答案 1 :(得分:1)
请按以下方式更改您的代码:
Calendar now = Calendar.getInstance();
TextView output3 = (TextView) findViewById(R.id.output3);
int dayno = now.get(Calendar.DAY_OF_WEEK);
String dayName = new String();// also tried String dayName = ""; with the same results
if (dayno == 1) dayName = "Sun";
if (dayno == 2) dayName = "Mon";
if (dayno == 3) dayName = "Tue";
if (dayno == 4) dayName = "Wed";
if (dayno == 5) dayName = "Thu";
if (dayno == 6) dayName = "Fri";
if (dayno == 7) dayName = "Sat";
答案 2 :(得分:0)
所有if子句检查dayno
是否为1
。将它们更改为正确的值或使用switch语句,它应该工作正常。对此的switch语句如下所示:
switch(dayno) {
case Calendar.SUNDAY:
dayName = "Sun";
break;
case Calendar.MONDAY:
dayName = "Mon";
break;
case Calendar.TUESDAY:
dayName = "Tue";
break;
case Calendar.WEDNESDAY:
dayName = "Wed";
break;
case Calendar.THURSDAY:
dayName = "Thu";
break;
case Calendar.FRIDAY:
dayName = "Fri";
break;
case Calendar.SATURDAY:
dayName = "Sat";
break;
}
答案 3 :(得分:0)
顺便说一句,您可以使用班级DateFormatSymbols,并避免重新发明轮子
Calendar now = Calendar.getInstance();
int dayno = now.get(Calendar.DAY_OF_WEEK);
String[] weekdays = new DateFormatSymbols().getWeekdays();
System.out.println(weekdays[dayno]);
,但让我们回到你的问题
你在代码中有这个
if (dayno == 1) dayName = "Sun";
if (dayno == 1) dayName = "Mon";
但如果dayno不是1怎么办? 然后 dayName 为空...您有一个复制/粘贴错误
你的意思是肯定的:
if (dayno == 1) {
dayName = "Sun"
} else if (dayno == 2) {
dayName = "Mon";
}...
等等
答案 4 :(得分:0)
int dayno = now.get(Calendar.DAY_OF_WEEK);
String dayName = new String();
switch(dayno) {
case 1: dayName = "Sun"; break;
case 2: dayName = "Mon"; break;
case 3: dayName = "Tue"; break;
case 4: dayName = "Wed"; break;
case 5: dayName = "Thu"; break;
case 6: dayName = "Fri"; break;
case 7: dayName = "Sat"; break;
default: dayName = "dayno is not [1, 7]"; break;
}
假设星期天是1 ...
答案 5 :(得分:0)
请勿使用开关或多个if语句,使用SimpleDateFormat
Calendar now = Calendar.getInstance();
DateFormat df = DateFormat.getDateTimeInstance();
// Or... df = new SimpleDateFormat("SOME-TIME-FORMAT");
String nowSting = df.format(now.getTime());
textView.setText(nowString);
我相信你的格式是这样的
hh:mm:ss Week w, EEE, yyyy
答案 6 :(得分:0)
使用SWITCH执行此操作。
Calendar now = Calendar.getInstance();
String dayName;
TextView output3 = (TextView) findViewById(R.id.output3);
int dayno = now.get(Calendar.DAY_OF_WEEK);
switch(dayno){
case 1: dayName = "Sunday"; break;
case 2: dayName = "Monday"; break;
case 3: dayName = "Tuesday"; break;
case 4: dayName = "Wednesday"; break;
case 5: dayName = "Thursday"; break;
case 6: dayName = "Friday"; break;
case 7: dayName = "Saturday"; break;
}