Android日历获取当前星期几作为string

我试图用实际工作日的名字来得到一个string:

Calendar c = Calendar.getInstance(); int dayOfWeek = c.get(Calendar.DAY_OF_WEEK); if (c.get(Calendar.MONDAY) == dayOfWeek) weekDay = "monday"; else if (c.get(Calendar.TUESDAY) == dayOfWeek) weekDay = "tuesday"; else if (c.get(Calendar.WEDNESDAY) == dayOfWeek) weekDay = "wednesday"; else if (c.get(Calendar.THURSDAY) == dayOfWeek) weekDay = "thursday"; else if (c.get(Calendar.FRIDAY) == dayOfWeek) weekDay = "friday"; else if (c.get(Calendar.SATURDAY) == dayOfWeek) weekDay = "saturday"; else if (c.get(Calendar.SUNDAY) == dayOfWeek) weekDay = "sunday"; 

weekDay总是空,我实际上不知道为什么,因为debugging器说, dayOfWeek是5,所以我应该等于c.get(Calendar.THURSDAY)

我通过以下操作完成了这个任务:

 String weekDay; SimpleDateFormat dayFormat = new SimpleDateFormat("EEEE", Locale.US); Calendar calendar = Calendar.getInstance(); weekDay = dayFormat.format(calendar.getTime()); 
  • 使用“EEEE”的date格式将返回完整的星期几名称,即星期二
  • 使用date格式为“E”将返回缩写名称,即星期二
  • 在Android中返回这种格式的另一个好处是它将基于语言环境来转换工作日。

您将需要查看SimpleDateFormat以了解更多信息。 我认为这是最干净的方法,你不需要开关或者如果你只需要获取string值的语句。

像这样简单

 sCalendar = Calendar.getInstance(); String dayLongName = sCalendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault()); 

你应该比较dayOfWeek直接与Calendar.MONDAY等请参阅下面的代码

另外,如果还有其他问题,我还会加上括号。 不要依赖缩进代码stream,即使if-else只有一个语句,也要明确地放上括号。

  public static void main(String[] args) { String weekDay = ""; Calendar c = Calendar.getInstance(); int dayOfWeek = c.get(Calendar.DAY_OF_WEEK); if (Calendar.MONDAY == dayOfWeek) { weekDay = "monday"; } else if (Calendar.TUESDAY == dayOfWeek) { weekDay = "tuesday"; } else if (Calendar.WEDNESDAY == dayOfWeek) { weekDay = "wednesday"; } else if (Calendar.THURSDAY == dayOfWeek) { weekDay = "thursday"; } else if (Calendar.FRIDAY == dayOfWeek) { weekDay = "friday"; } else if (Calendar.SATURDAY == dayOfWeek) { weekDay = "saturday"; } else if (Calendar.SUNDAY == dayOfWeek) { weekDay = "sunday"; } System.out.println(weekDay); } 

只需执行以下操作:

  Date date = new Date(); CharSequence time = DateFormat.format("EEEE", date.getTime()); // gives like (Wednesday) 

没有必要使用c.get

  Calendar c = Calendar.getInstance(); int dayOfWeek = c.get(Calendar.DAY_OF_WEEK); if (Calendar.MONDAY == dayOfWeek) weekDay = "monday"; else if (Calendar.TUESDAY == dayOfWeek) weekDay = "tuesday"; else if (Calendar.WEDNESDAY == dayOfWeek) weekDay = "wednesday"; else if (Calendar.THURSDAY == dayOfWeek) weekDay = "thursday"; else if (Calendar.FRIDAY == dayOfWeek) weekDay = "friday"; else if (Calendar.SATURDAY == dayOfWeek) weekDay = "saturday"; else if (Calendar.SUNDAY == dayOfWeek) weekDay = "sunday"; System.out.println(weekDay); 

和输出:

 thursday 

你可以看到你的错误,如果你试图用这个代码与c.get打印值

  System.out.println(c.get(Calendar.MONDAY)); System.out.println(c.get(Calendar.TUESDAY)); System.out.println(c.get(Calendar.WEDNESDAY)); System.out.println(c.get(Calendar.THURSDAY)); System.out.println(c.get(Calendar.FRIDAY)); System.out.println(c.get(Calendar.SATURDAY)); System.out.println(c.get(Calendar.SUNDAY)); 

例如,我得到:

 7 33 3 15 227 5 2013 

结果将是不正确的,在我的情况下,如果我使用您的代码,我得到Sunday作为weekDay

尝试这个

 import java.text.DateFormatSymbols; import java.util.Date; ... String[] weekdays = new DateFormatSymbols().getWeekdays(); String[] month = new DateFormatSymbols().getMonths(); System.outprintln(weekdays[date.getDay()+1]+","+month[date.getMonth()]+" "+date.getDate()); 

希望这个帮助你。 🙂