1. 程式人生 > >Java——根據日期返回周幾

Java——根據日期返回周幾

   /** 
    * 判斷當前日期是星期幾<br> 
    * <br> 
    * @param pTime 修要判斷的時間<br> 
    * @return dayForWeek 判斷結果<br> 
    * @Exception 發生異常<br> 
    */  
public static int dayForWeek(String pTime) throws Exception {
//注意引數的樣式為yyyy-MM-dd,必須讓引數樣式與所需樣式統一  
 format = new SimpleDateFormat("yyyy-MM-dd"
); Calendar c = Calendar.getInstance(); c.setTime(format.parse(pTime)); int dayForWeek = 0; if(c.get(Calendar.DAY_OF_WEEK) == 1){ dayForWeek = 7; }else{ dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1; } return dayForWeek; }