从 Java 中的格里高利历确定月份中的某一天星期几
如需使用 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(); // date information 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)); // week System.out.println("Day of Week in month = " + cal.get(Calendar.DAY_OF_WEEK_IN_MONTH)); } }
输出
Date Information.......... Year = 2018 Month = 11 Date = 19 Day of Week in month = 3
广告