Calendar cal=Calendar.getInstance();
     System.out.println(cal.YEAR);//本来是2005年,为什么打出来的是"1"
     System.out.println(cal.MONTH);//本来是7月,为什么打出来的是"2"
     System.out.println(cal.DAY_OF_MONTH);//本来是19号,为什么打出来的是"5"
     System.out.println(cal.DATE);//本来是19号,为什么打出来的是"5"
                                  //其中cal.DAY_OF_MONTH和cal.DATE是不是都是日期
     
     Date date=new Date();
     System.out.println(date.getYear());//本来是2005年,为什么打出来的是"105"
     System.out.println(date.getMonth());//本来是7月,为什么打出来的是"6"
     System.out.println(date.getDay());//本来是19号,为什么打出来的是"2"
     System.out.println(date.getDate());//getDate和getDay有什么区别,得到的都是今天几号??

解决方案 »

  1.   

    2. t=Calendar.getInstance();m=t.get(t.MONTH)+1;这里为什么要加一?   在java语言里,date的month的取值范围是:0~11,与人们的自然表达上相差1。
      

  2.   

    cal.YEAR 这些是calendar定义的常量,见文档:
    Field number for get and set indicating the year. This is a calendar-specific value; date.getYear()这些方法已经不推荐使用了,可以用DateFormat来格式化date得到:
    String format = "";
    SimpleDateFormat formatter = new SimpleDateFormat(format);
     formatter.format(date);具体格式javadoc里有讲的。比如得到当前年份,format="yyyy".
      

  3.   

    楼主得到的1、2之类的数字而没有得到当前时间的具体年、月等等,是因为YEAR、DAY都是Calendar的静态常量,当然是不会变的,你可以用Calendar.YEAR,Calendar.MONTH等测试一下,
    可以参考下面的例子:
    GregorianCalendar cal = new GregorianCalendar();
    System.out.println(""
    +cal.get(GregorianCalendar.YEAR)
    +cal.get(GregorianCalendar.MONTH)+1//月份是0-11
    +cal.get(GregorianCalendar.DAY_OF_MONTH)
    +cal.get(GregorianCalendar.HOUR_OF_DAY)
    +cal.get(GregorianCalendar.MINUTE)
    +cal.get(GregorianCalendar.SECOND)
    );
      

  4.   

    1、
    public final static int YEAR = 1;
    public final static int MONTH = 2;
    public final static int DAY_OF_MONTH = 5;
    public final static int DATE = 5;这是在Calendar中定义的,要不是你那里的结果才是怪事情呢,
    你可以这样得到年份:System.out.println(cal.get(cal.YEAR));2、
    System.out.println(date.getYear());//本来是2005年,为什么打出来的是"105"
    从1900年算起: 2005-1900 = 105
    System.out.println(date.getMonth());//本来是7月,为什么打出来的是"6"
    月份从0开始,6表示是7月System.out.println(date.getDay());//本来是19号,为什么打出来的是"2"
    今天是星期几System.out.println(date.getDate());//
    这个才得到是一个月的多少号
      

  5.   

    要得到正确的值应该用get(int field)方法,参数就是Calendar的那些静态常量,如
    Calendar cal = Calendar.getInstance();
    System.out.println(cal.get(Calendar.YEAR));楼主应该明白了吧
      

  6.   

    你应该这样用
    Calendar cal  = new GregorianCalendar();
    System.out.println(cal.get(Calendar.YEAR));
    System.out.println(cal.get(Calendar.MONTH));
    System.out.println(cal.get(Calendar.DAY_OF_MONTH));
    System.out.println(cal.get(Calendar.DATE));
    //月份必须加1
    //System.out.println(cal.get(Calendar.YEAR)+"年"+(cal.get(Calendar.MONTH)+1)+"月"+cal.get(Calendar.DAY_OF_MONTH)+"日");
      

  7.   

    Calendar cal=Calendar.getInstance();
        //注意和下面的比较,cal.YEAR只是作为field参数值,对于任何一天都一样
        System.out.println(cal.get(cal.YEAR)); 
        System.out.println(cal.get(cal.MONTH)); 
        System.out.println(cal.get(cal.DAY_OF_MONTH)); 
        System.out.println(cal.get(cal.DATE)); 
        
         System.out.println(cal.YEAR);//本来是2005年,为什么打出来的是"1"
         System.out.println(cal.MONTH);//本来是7月,为什么打出来的是"2"
         System.out.println(cal.DAY_OF_MONTH);//本来是19号,为什么打出来的是"5"
         System.out.println(cal.DATE);//本来是19号,为什么打出来的是"5"
                                      //其中cal.DAY_OF_MONTH和cal.DATE是不是都是日期     Date date=new Date();
         System.out.println(date.getYear() + 1900);//从1900年开始计算的
         System.out.println(date.getMonth()+1);//月份是从0开始算的,所以要加1
         System.out.println(date.getDay());//这里是星期,相当于Calendar.get(Calendar.DAY_OF_WEEK)
         System.out.println(date.getDate());//getDate和getDay有什么区别,得到的都是今天几号??
      

  8.   

    Calendar cal=Calendar.getInstance();
    System.out.println(cal.get(cal.YEAR));//本来是2005年,为什么打出来的是"1"
    System.out.println(cal.get(cal.MONTH)+1);//本来是7月,为什么打出来的是"2"
    System.out.println(cal.get(cal.DAY_OF_MONTH));//本来是19号,为什么打出来的是"5"
    System.out.println(cal.get(cal.DATE));//本来是19号,为什么打出来的是"5"
     //其中cal.DAY_OF_MONTH和cal.DATE是不是都是日期
    //cal.DATE 显示当天的是几号
    //cal.DAY_OF_MONTH 显示当天在当月中是第几天
    Date date=new Date();
    getYear()  
    //Returns a value that is the result of subtracting 1900 from the year that contains or begins with the instant in time represented by this Date object, as interpreted in the 
    local time zone. 
    getMonth()
    //Returns a number representing the month that contains or begins with the instant in time represented by this Date object. The value returned is between 0 and 11, with the value 0 representing January. 
    getDay()
    //Returns the day of the week represented by this date. The returned value (0 = Sunday, 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday, 5 = Friday, 6 = Saturday) represents the day of the week that contains or begins with the instant in time represented by this Date object, as interpreted in the local time zone. 
    getDate()
    //Returns the day of the month represented by this Date object. The value returned is between 1 and 31 representing the day of the month that contains or begins with the instant in time represented by this Date object, as interpreted in the local time zone.
      

  9.   

    建议楼主看看JAVA SE 的 DOC和SRC。现做一简单的解释:
    Calendar类中的ERA/YEAR/MONTH/WEEK_OF_YEAR/WEEK_OF_MONTH等属性,均为static int,表示的是这些属性Calendar类中实际存储数据的数组(protected int fields[])中的索引号(index),在你的程序中,可以使用
    cal.get(Calendar.YEAR)
    来得到年份。其它的字段的内容类似。
    有一点是和其它的内容不一样的,就是MONTH,这个字段的返回值是Calendar.JANUARY到Calendar.UNDECEMBER的一个值。它的实际值是0到12这十三个值,具体这些值是什么意思请你看看DOC或SRC。
    DATE和DAY_OF_MONTH的意思相同,这个在SRC中这样说了一下:
        /**
         * Field number for <code>get</code> and <code>set</code> indicating the
         * day of the month. This is a synonym for <code>DATE</code>.
         * The first day of the month has value 1.
         * @see #DATE
         */Date类中有一点和Calendar类不一样,就是它的年份是从1900年记起的一个相对年份,所以今年是2005 = 1900 + 105,所以打出来的年份是105。
    月份的值如上面的Calendar类。
    最后的问题:Day就是星期几,今天是星期二,所以就打出来TUESDAY常量的值了,它是2所以……DATE才是这个月真正的日期。
      

  10.   

    Java编译器应该像C#那样禁止通过对象访问静态成员比如楼主的cal.DATE
      

  11.   

    http://java.sun.com/j2se/1.4.2/docs/api/java/util/Date.html
    http://java.sun.com/j2se/1.4.2/docs/api/java/util/Calendar.html
      

  12.   

    老兄,给你个例子看看
    在最近的一个OA中,我需要判断两个日期是否是同一周,根据一个给定的日期获得所属周的周一和周五的日期。在完成以上任务时,我发现Calendar 的确是一个功能强大的class。下面给出源码,供兄弟们参考指正。
    /*
     * 创建日期 2005-3-30package com.infoearth;import java.sql.Timestamp;
    import java.text.SimpleDateFormat;
    import java.util.*;
    /**
     * @李春雷
     *
    public class ManageWeek {
     //判断两个日期是否在同一周
     boolean isSameWeekDates(Date date1, Date date2) {
      Calendar cal1 = Calendar.getInstance();
      Calendar cal2 = Calendar.getInstance();
      cal1.setTime(date1);
      cal2.setTime(date2);
      int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
      if (0 == subYear) {
        if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
       return true;
      }
      else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
        // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
        if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
       return true;
      }
      else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
        if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
       return true;
      }
      return false;
     }
     
     
     //产生周序列
     public static String  getSeqWeek(){
      Calendar c = Calendar.getInstance(Locale.CHINA);
      String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
      if(week.length()==1)week = "0" + week;
      String year = Integer.toString(c.get(Calendar.YEAR));  
      return year+week;
      
     }
      
      //获得周一的日期
      public static String getMonday(Date date){
       Calendar c = Calendar.getInstance();
       c.setTime(date);
       c.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
       return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
      }
      
      
      //获得周五的日期
      public static String getFriday(Date date){
       Calendar c = Calendar.getInstance();
       c.setTime(date);
       c.set(Calendar.DAY_OF_WEEK,Calendar.FRIDAY);   
       return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
       
       
      }
      
      public static void main(String[] args){  }}
      

  13.   

    哪里有现成的现行农历的 LunarCalendar 类么?