欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Date,DateFormat,Calendar类的使用

程序员文章站 2022-05-09 20:14:08
[html]  package com.itheima.date;    import java.util.Calendar; ...

[html] 
package com.itheima.date; 
 
import java.util.Calendar; 
 
public class CalendarDemo1 { 
    public static void main(String[] args) { 
        Calendar c = Calendar.getInstance(); 
        c.set(2012, 11, 16); //设置时间从0开始 ,0 ---一月 
        c.add(Calendar.MONTH, 4);  //增量。 
         
        printCalendar(c); 
         
    }    
    private static void sop(Object obj) { 
        System.out.println(obj); 
    } 
    private static  void printCalendar(Calendar c) { 
        /* 
         * 使用查表法获取中文月,星期 
         */ 
        String[] mons = { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", 
                "十月", "十一月", "十二月" }; 
        String[] weeks = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; 
        int index = c.get(Calendar.MONTH); 
        int index1 = c.get(Calendar.DAY_OF_WEEK); 
 
        sop(c.get(Calendar.YEAR) + "年"); 
//      sop((c.get(Calendar.MONTH) + 1) + "月"); 
        sop(mons[index]); 
        sop(c.get(Calendar.DAY_OF_MONTH)+"日"); 
//      sop("星期"+c.get(Calendar.DAY_OF_WEEK)); 
        sop(weeks[index1]); 
 
    } 
 
    }