对于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