Calendar类:
Calendar中其实也是维护了一个Map集合,根据对应的键来拿值
import java.util.Calendar;
public class CalendarDemo {
public static void main(String[] args) {
/*
* 演示日历
* Calendar
*/
Calendar c = Calendar.getInstance();//c里面维护了一个map集合,键值
//get方法,按照键拿值
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH)+1;//月份在计算机中表示的是从0-11月
int day = c.get(Calendar.DAY_OF_MONTH);
String week = getWeek(c.get(Calendar.DAY_OF_WEEK));//星期日为一周的第一天
System.out.println(year+"年"+month+"月"+day+"日"+week);
}
private static String getWeek(int i) {
if(i<=0||i>7)
throw new RuntimeException(i+"错误的星期");
String [] s = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
return s[i];
}
}