在Java中获取特定日期的星期几
要获取Java中特定日期的星期几,请使用Calendar.DAY_OF_WEEK常量。
让我们先设置一个日期。
Calendar one = new GregorianCalendar(2010, Calendar.JULY, 10);
由于我们已经使用了Calendar以及GregorianCalendar类,因此,首先,请导入以下软件包。
import java.util.Calendar; import java.util.GregorianCalendar;
现在,我们将查找星期几。
int day = one.get(Calendar.DAY_OF_WEEK);
以下是一个例子
示例
import java.util.Calendar; import java.util.GregorianCalendar; public class Demo { public static void main(String[] argv) throws Exception { Calendar one = new GregorianCalendar(2010, Calendar.JULY, 10); int day = one.get(Calendar.DAY_OF_WEEK); System.out.println(day); Calendar two = new GregorianCalendar(2011, Calendar.AUGUST, 9); day = two.get(Calendar.DAY_OF_WEEK); System.out.println(day); Calendar three = new GregorianCalendar(2017, Calendar.OCTOBER, 22); day = three.get(Calendar.DAY_OF_WEEK); System.out.println(day); Calendar four = new GregorianCalendar(2018, Calendar.DECEMBER, 30); day = four.get(Calendar.DAY_OF_WEEK); System.out.println(day); } }
输出结果
7 3 1 1