java-Calendar类

1、Calendar类的概述和获取日期的方法
  * A:Calendar类的概述
    * Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
  * B:成员方法
    * public static Calendar getInstance()
    * public int get(int field)

2、Calendar类的add()和set()方法
  * A:成员方法
    * public void add(int field,int amount)
    * public final void set(int year,int month,int date)

例:

 public class Demo {

     public static void main(String[] args) {
//demo1();
Calendar c = Calendar.getInstance();
// c.add(Calendar.MONTH, -1);
// c.set(Calendar.YEAR, 2008);
c.set(2000,7,8);
System.out.println(c.get(Calendar.YEAR) + "年"
+ getNum(c.get(Calendar.MONTH) + 1) + "月"
+ getNum(c.get(Calendar.DAY_OF_MONTH)) + "日"
+ getWeek(c.get(Calendar.DAY_OF_WEEK)));
} private static void demo1() {
Calendar c = Calendar.getInstance();
// System.out.println(c);
System.out.println(c.get(Calendar.YEAR));
System.out.println(c.get(Calendar.MONTH));
System.out.println(c.get(Calendar.DAY_OF_MONTH));
System.out.println(c.get(Calendar.YEAR) + "年"
+ getNum(c.get(Calendar.MONTH) + 1) + "月"
+ getNum(c.get(Calendar.DAY_OF_MONTH)) + "日"
+ getWeek(c.get(Calendar.DAY_OF_WEEK)));
} public static String getWeek(int week) {
String[] arr = { "", "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; return arr[week];
} public static String getNum(int num) {
// if(num>9){
// return ""+num;
// }else{
// return "0"+num;
// } return num > 9 ? "" + num : "0" + num;
} }
上一篇:Javascript 生成随机数


下一篇:[带你飞]一小时带你学会Python