Android : 设置日期并获取星期几
Android : Set the date and get day of week
我有问题
设置日期并获取星期几
设置 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;
}
请记住 java 日历中的一月应该是 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;
}
你可以用这个
Calendar cal = Calendar.getInstance();
cal.set(2011, 0, 17);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE",Locale.getDefault());
String dayInStrinFormat = sdf.format(cal.getTime());
1 月为 0 .... 12 月为 11
我有问题 设置日期并获取星期几
设置 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;
}
请记住 java 日历中的一月应该是 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;
}
你可以用这个
Calendar cal = Calendar.getInstance();
cal.set(2011, 0, 17);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE",Locale.getDefault());
String dayInStrinFormat = sdf.format(cal.getTime());
1 月为 0 .... 12 月为 11