要使用Calendar类,请导入以下程序包。import java.util.Calendar;
使用Calendar类,创建一个对象。Calendar calendar = Calendar.getInstance();
现在,创建一个包含日期名称的字符串数组。String[] days = new String[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
显示日期名称。days[calendar.get(Calendar.DAY_OF_WEEK) - 1]
以下是最后一个示例。
示例import java.util.Calendar;
public class Demo {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
System.out.println("Day: " + (calendar.get(Calendar.DATE)));
System.out.println("Month: " + (calendar.get(Calendar.MONTH) + 1));
System.out.println("Year: " + (calendar.get(Calendar.YEAR)));
String[] days = new String[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
System.out.println("Current day = " + days[calendar.get(Calendar.DAY_OF_WEEK) - 1]);
}
}
输出结果Day: 19
Month: 11
Year: 2018
Current day = Monday