问题
This question already has an answer here:
- Android: how to get the current day of the week (Monday, etc…) in the user's language? 10 answers
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)
回答1:
As simple as this
sCalendar = Calendar.getInstance();
String dayLongName = sCalendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());
回答2:
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.
You will want to review the SimpleDateFormat to learn more. I think this is the cleanest approach in that you do not need a switch or if statement if your only need is to get the string value.
回答3:
You are supposed to compare dayOfWeek directly with Calendar.MONDAY etc. See code below
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.
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);
}
回答4:
There was no need to use the 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
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.
回答5:
just do the following:
Date date = new Date();
CharSequence time = DateFormat.format("EEEE", date.getTime()); // gives like (Wednesday)
回答6:
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());
来源:https://stackoverflow.com/questions/18256521/android-calendar-get-current-day-of-week-as-string