从 GregorianCalendar 获取星期几的 Java 程序
对于 GregorianCalendar 类,导入以下包。
import java.util.GregorianCalendar;
创建一个对象。
GregorianCalendar calendar = new GregorianCalendar();
若要获取星期几,请使用以下字段。
GregorianCalendar.DAY_OF_WEEK
下面是一个示例。
示例
import java.util.Calendar; import java.util.GregorianCalendar; public class Demo { public static void main(String[] a) { GregorianCalendar calendar = new GregorianCalendar(); System.out.println("Day of Week = " + calendar.get(GregorianCalendar.DAY_OF_WEEK)); System.out.println("Date = " + calendar.get(GregorianCalendar.DATE)); System.out.println("Month = " + calendar.get(GregorianCalendar.MONTH)); System.out.println("Year = " + calendar.get(GregorianCalendar.YEAR)); } }
输出
Day of Week = 2 Date = 19 Month = 10 Year = 2018
广告