方法1:
/**<br /> * 判斷當前日期是星期幾<br><br /> * <br><br /> * @param pTime 修要判斷的時間<br><br /> * @return dayForWeek 判斷結果<br><br /> * @Exception 發生異常<br><br /> */<br /> public static int dayForWeek(String pTime) throws Exception {<br /> format = new SimpleDateFormat("yyyy-MM-dd");<br /> Calendar c = Calendar.getInstance();<br /> c.setTime(format.parse(pTime));<br /> int dayForWeek = 0;<br /> if(c.get(Calendar.DAY_OF_WEEK) == 1){<br /> dayForWeek = 7;<br /> }else{<br /> dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;<br /> }<br /> return dayForWeek;<br /> }
方法2:
需要匯入的包
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public static int dayForWeek(String pTime) throws Throwable {</p><p>SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");</p><p>Date tmpDate = format.parse(pTime);</p><p>Calendar cal = new GregorianCalendar();</p><p>cal.set(tmpDate.getYear(), tmpDate.getMonth(), tmpDate.getDay());</p><p>return cal.get(Calendar.DAY_OF_WEEK);<br />}