世事洞明皆学问,人情练达即文章。这篇文章主要讲述Android日历以字符串[复制]获取当前星期几相关的知识,希望能为你提供帮助。
这个问题在这里已有答案:
- Android: how to get the current day of the week (Monday, etc…) in the user's language? 8个答案
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
总是保持为空,我实际上不知道为什么因为调试器说dayOfWeek
是5所以我应该等于c.get(Calendar.THURSDAY)
答案就这么简单
sCalendar = Calendar.getInstance();
String dayLongName = sCalendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());
另一答案我通过执行以下操作完成了此操作:
String weekDay;
SimpleDateFormat dayFormat = new SimpleDateFormat("EEEE", Locale.US);
Calendar calendar = Calendar.getInstance();
weekDay = dayFormat.format(calendar.getTime());
- 使用“EEEE”的日期格式将返回完整的工作日名称,即星期二
- 使用“E”的日期格式将返回缩写名称,即Tue
- 在android中返回此格式的另一个好处是它将根据区域设置翻译工作日。
另一答案您应该将dayOfWeek直接与Calendar.MONDAY等进行比较。请参阅下面的代码
另外,如果是其他的话,我已将括号括起来。不要依赖缩进代码流,即使你的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);
}
另一答案没有必要使用
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);
【Android日历以字符串[复制]获取当前星期几】并输出:
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
。另一答案只需执行以下操作:
Date date = new Date();
CharSequence time = DateFormat.format("EEEE", date.getTime());
// gives like (Wednesday)
另一答案试试这个
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());
推荐阅读
- 从页面操作中获取标签网址(WebExtensions,Android)
- 如何在应用程序超时时重新启动Coldfusion Application Server()
- Xamarin UITest - 确定设备是适用于Android的手机还是平板电脑()
- Apple App如何获得YouTube授权
- Android WebView从资源原始文件夹加载html文件
- Xamarin Android(为什么重写OnSaveInstanceState永远不会被调用())
- Xamarin Android通过一种风格控制文本颜色
- Xama不会跟踪Xamarin Android新文件
- 我是否需要所有Android SDK以及如何安装它们()