请问,jdk中calendar类有得到当前月,共有多少天的方法吗,如果没有,其他办法怎么实现,
我想日期计算,比如+_等,怎样实现.

解决方案 »

  1.   

    月份要加1.你打印如下:
    Calendar cl = Calendar.getInstance();
    cl.set(2005, 11, 20);
    cl.add(cl.DAY_OF_YEAR, 20);
    System.out.println(cl.get(cl.YEAR) + "年" + cl.get(cl.MONTH) + "月"
    + cl.get(cl.DAY_OF_MONTH) + "日");
    //输出:2006年0月9日  说明是月份是从0开始的
      

  2.   

    如果是用ORACLE可以这样实现。
    select substr(to_char(sysdate-1,'yyyyMMdd')) from dual;
      

  3.   

    select substr(to_char(sysdate-1,'yyyyMMdd'),5) from dual;
      

  4.   

    java.util.calendar.getActualMaximum()
    public int getActualMaximum(int field)Return the maximum value that this field could have, given the current date. For example, with the date "Feb 3, 1997" and the DAY_OF_MONTH field, the actual maximum would be 28; for "Feb 3, 1996" it s 29. Similarly for a Hebrew calendar, for some years the actual maximum for MONTH is 12, and for others 13. The version of this function on Calendar uses an iterative algorithm to determine the actual maximum value for the field. There is almost always a more efficient way to accomplish this (in most cases, you can simply return getMaximum()). GregorianCalendar overrides this function with a more efficient implementation.
      

  5.   

    long java.util.Calendar.getTimeInMillis() to get the milliseconds of a date based on 1970/1/1 00hrs00minutes00secondsfor example: if you want to know how many days are there in 1984/10, then you create two objects of Calendar of the date as 1984/11/1 and 1984/10/1, then use the above mentioned method to get milliseconds value of each, substract then divided by milliseconds of a day, that is 3600*24, then the days of 1984/10 comes to you.it's just general idea, implementation won't be a problem after you get this idea
      

  6.   

    /*
     * Created on 2006-3-5
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    package com.pss.test;import java.util.*;/**
     * @author George
     * 
     * TODO To change the template for this generated type comment go to Window -
     * Preferences - Java - Code Style - Code Templates
     */
    public class Test3 {
    public void plus(int year, int month) {
    Calendar c1 = Calendar.getInstance();
    c1.set(year, month - 1, 1);

    Calendar c2 = Calendar.getInstance();
    c2.set(year, month, 1);

    long c2s = c2.getTimeInMillis();
    long c1s = c1.getTimeInMillis(); long day = 1 * 24 * 60 * 60 * 1000; long days = (c2s - c1s) / day; System.out.println(year + "年" + month + "月有" + days + "天");
    } public static void main(String[] args) {
    Test3 test = new Test3();
    test.plus(2005,2);
    }
    }
      

  7.   

    absolutely right...sorry I forgot it's milliseconds, one day is 24*3600*1000
      

  8.   

    上的方法有个问题:
    getTimeInMillis() is protected  access in java.util.Calendar!
      

  9.   

    参照Calendar类,一定有方法,你可以看一下
    大概流程是:
    Calendar c = Calendar.getInstance();
    SimpleDateFormatter sdf = new SimpleDateFormatter("yyyy-MM");
    c.setTime(sdf.parse("2006-03"));
    c.get(Calendar.DAYOFMONTH);//这个好像是这样,记不清楚了
      

  10.   

    int y = 2000;//年
            int m = 2;//月
            System.out.println(new Date(y - 1900, m, 0).getDate());
      

  11.   

    http://blog.csdn.net/netcom19/archive/2004/12/08/209498.aspx
      

  12.   

    getTimeInMillis() is definitely public modifier...Calendar.DAY_OF_MONTH represents which day of that month the date is, it isn't the number of days for that month.
      

  13.   

    to 楼上的外宾:
    我的程序一概没问题,但是编译时提示getTimeInMillis() is protected  access in java.util.Calendar!
    代码如下:
    import java.util.*;public class TestCalendar
    {
    public void days(int year,int month)
    {
    Calendar c1 = Calendar.getInstance();
    Calendar C2 = Calendar.getInstance();
    c1.set(year,month,1);
    c2.set(year,month-1,1);
    long c1m = c1.getTimeInMillis();
    long c2m = c2.getTimeInMillis();
    long dm =24*60*60*1000;
    long days = (c1m-c2m)/dm;
    System.out.println(year+"年"+month+"月"+"有"+"days"+"天");
    }
    public static void main(String[] args)
    {
        TestCalendar t = new TestCalendar();
        t.days(2006,2); 
    }
    }
      

  14.   

    sorry,不是一概,是应该没问题!
    不能太大话了!
      

  15.   

    我前面已经给出答案了,绝对没有问题,写成个方法吧
    public class aaa {
        public static void main(String[] args) {
            System.out.println(getdays(2000, 2));
        }    public static int getdays(int y, int m) {//y年m月
            return new Date(y - 1900, m, 0).getDate();
        }
    }
      

  16.   

    大花猫的方法值得学习.
    请允许我擅自修改一下:
    Calendar c01 = Calendar.getInstance();
    c01.set(year, month, 0);

    int dayOfM = c01.get(Calendar.DAY_OF_MONTH);
    System.out.println(year + "年" + month + "月有" + dayOfM + "天");
      

  17.   

    值得学习吗?
    你调试了吗?
    DAY_OF_MONTH得到的是某月的某天
    而不是某月的天数他说的是要自己写个方法, getTimeInMillis()是可行的
    但是我的程序有错误提示,希望大家看看!
      

  18.   

    提出另外一种思考方法。
    Calendar类中是没有直接得到某月的天数的方法,但是可以得到某月的某天。所以如果这一天是该月的最后一天就可以得到该月的天数。得到某月的最后一天比较麻烦,每月天数不一样。但是可以得到下一月的第一天,然后再减去一天就是该月的最后一天.
    public static void main(String[] arg){
        
        Calendar calendar = Calendar.getInstance();
        
        //get the first day of this month
        calendar.set(Calendar.DAY_OF_MONTH, 1);    //get the first day of next month
        calendar.add(Calendar.MONTH, 1);    //get the last day of this month
        calendar.add(Calendar.DATE, -1);    //Print the days of this month
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
    }
      

  19.   

    汗!刚发现有直接的方法。
    System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
      

  20.   

    Good discussion, I found some many ways to get number of days in a given month