Java Android Calendar 获取当前星期几作为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18256521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Calendar get current day of week as string
提问by ntm
i tried to get a string with the name of the actual weekday this way:
我试图以这种方式获取带有实际工作日名称的字符串:
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";
but weekDay
stays always null and i actually have no idea why because the debugger says that dayOfWeek
is 5 so i should be equal to c.get(Calendar.THURSDAY)
但weekDay
始终为空,我实际上不知道为什么,因为调试器说这dayOfWeek
是 5,所以我应该等于c.get(Calendar.THURSDAY)
回答by arun_gopalan
You are supposed to compare dayOfWeek directly with Calendar.MONDAY etc. See code below
您应该将 dayOfWeek 直接与 Calendar.MONDAY 等进行比较。请参阅下面的代码
Also, I have put brackets around if else. Do not rely on indentation for code flow, explicitly put brackets even if your if-else has only one statement.
另外,我在 if else 周围放了括号。不要依赖缩进来处理代码流,即使你的 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);
}
回答by Oscerd
There was no need to use the c.get
没有必要使用 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);
and output:
和输出:
thursday
You can see your error if you try to print the values with c.get
with this code
如果您尝试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));
For example I get:
例如我得到:
7
33
3
15
227
5
2013
And the result will be incorrect, in my case I get Sunday
as weekDay
if I use your code.
其结果将是不正确的,在我的情况,我得到Sunday
的weekDay
,如果我用你的代码。
回答by gifpif
try this
尝试这个
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());
回答by Don Gossett
I accomplished this by doing the following:
我通过执行以下操作来完成此操作:
String weekDay;
SimpleDateFormat dayFormat = new SimpleDateFormat("EEEE", Locale.US);
Calendar calendar = Calendar.getInstance();
weekDay = dayFormat.format(calendar.getTime());
- Use the day format for "EEEE" will return the full weekday name, i.e. Tuesday
- Use the day format for "E" will return the the abbreviated name, i.e. Tue
- Another benefit to returning this format in Android is that it will translate the weekday based on the locale.
- 使用“EEEE”的日期格式将返回完整的工作日名称,即星期二
- 使用“E”的日期格式将返回缩写名称,即 Tue
- 在 Android 中返回此格式的另一个好处是它将根据区域设置转换工作日。
You will want to review the SimpleDateFormatto learn more. I think this is the cleanest approach in that you do not need a switchor ifstatement if your only need is to get the string value.
您将需要查看SimpleDateFormat以了解更多信息。我认为这是最干净的方法,因为如果您只需要获取字符串值,则不需要switch或if语句。
回答by sandalone
As simple as this
就这么简单
sCalendar = Calendar.getInstance();
String dayLongName = sCalendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());
回答by Muhammed Refaat
just do the following:
只需执行以下操作:
Date date = new Date();
CharSequence time = DateFormat.format("EEEE", date.getTime()); // gives like (Wednesday)