需求:页面传来一个年月日的字符串,根据这个字符串判断这一天是星期几
代码:
public class TestCalender {
@Test
public void testCalender() throws Exception{
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(format.parse("2016-3-21")); //判断方法
int dayForWeek = 0;
if(c.get(Calendar.DAY_OF_WEEK) == 1){
dayForWeek = 7;
}else{
dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
}
System.out.println(dayForWeek);
}
}
结果:1