要使用GregorianCalendar类,请导入以下包-
import java.util.GregorianCalendar;
要获取每月的星期几,请使用以下字段-
cal.get(Calendar.DAY_OF_WEEK_IN_MONTH)
上面的cal是我们在之前创建的GregorianCalendar对象-
GregorianCalendar cal = new GregorianCalendar();
以下是一个例子-
import java.util.GregorianCalendar; import java.util.Calendar; import java.util.Date; public class Demo { public static void main(String[] args) { GregorianCalendar cal = new GregorianCalendar(); //日期信息 System.out.println("Date Information.........."); System.out.println("Year = " + cal.get(Calendar.YEAR)); System.out.println("Month = " + (cal.get(Calendar.MONTH) + 1)); System.out.println("Date = " + cal.get(Calendar.DATE)); //周 System.out.println("Day of周 in month = " + cal.get(Calendar.DAY_OF_WEEK_IN_MONTH)); } }
输出结果
Date Information.......... Year = 2018 Month = 11 Date = 19 Day of周 in month = 3