我有问题 设置日期并获取星期几
设置2011/1/17 ==>星期一
Date d = new Date();
d.setDate(17);
d.setMonth(1);
d.setYear(2011);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
String dayOfWeek = sdf.format(d);
/ * - - - - - - - - - - 或 - - - - - - - - - - - - - - - * /
Calendar c = Calendar.getInstance();
c.set(2011, 1, 17);
int day = c.get(Calendar.DAY_OF_WEEK);
String weekDay="";
switch (day) {
case Calendar.SUNDAY: weekDay = "Sunday"; break;
case Calendar.MONDAY: weekDay = "Monday"; break;
case Calendar.TUESDAY: weekDay = "Tuesday"; break;
case Calendar.WEDNESDAY: weekDay = "Wednesday"; break;
case Calendar.THURSDAY: weekDay = "Thursday"; break;
case Calendar.FRIDAY: weekDay = "Friday"; break;
case Calendar.SATURDAY: weekDay = "Saturday"; break;
}
答案 0 :(得分:1)
请记住,在Java日历中,1月应为0。设置2011/01/17应该是2011/00/17。
c.set(2011, 0, 17);
//or c.set(2011, Calendar.JANUARY, 17);
int day = c.get(Calendar.DAY_OF_WEEK);
String weekDay="";
switch (day) {
case Calendar.SUNDAY: weekDay = "Sunday"; break;
case Calendar.MONDAY: weekDay = "Monday"; break;
case Calendar.TUESDAY: weekDay = "Tuesday"; break;
case Calendar.WEDNESDAY: weekDay = "Wednesday"; break;
case Calendar.THURSDAY: weekDay = "Thursday"; break;
case Calendar.FRIDAY: weekDay = "Friday"; break;
case Calendar.SATURDAY: weekDay = "Saturday"; break;
}
答案 1 :(得分:1)
你可以用这个
Calendar cal = Calendar.getInstance();
cal.set(2011, 0, 17);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE",Locale.getDefault());
String dayInStrinFormat = sdf.format(cal.getTime());