CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
IBM Rational 系统开发最佳实践工具包 WebSphere MQ 最佳实践 TOP 15
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  J2SE / 基础类

如何得到某年某月共有多少天?急

楼主tanghulu(糖葫芦)2006-03-05 10:51:51 在 Java / J2SE / 基础类 提问

请问,jdk中calendar类有得到当前月,共有多少天的方法吗,如果没有,其他办法怎么实现,  
  我想日期计算,比如+_等,怎样实现. 问题点数:99、回复次数:25Top

1 楼lydvqq(碧水情缘♀黑哥)回复于 2006-03-05 11:02:32 得分 99

月份要加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开始的Top

2 楼classjava(原始野人)回复于 2006-03-05 11:02:57 得分 0

第二个月的1号去减前面一个月的时间Top

3 楼zhonghua2003(爱我中华)回复于 2006-03-05 11:20:22 得分 0

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

4 楼zhonghua2003(爱我中华)回复于 2006-03-05 11:20:44 得分 0

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

5 楼human_2(风一帆)回复于 2006-03-05 11:31:45 得分 0

第二个月的1号去减前一个月一号   -1  
  譬如3月天数  
  4月1号-2月1号-1Top

6 楼wizardblue()回复于 2006-03-05 12:10:22 得分 0

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.Top

7 楼sportboys(多动男孩)回复于 2006-03-05 13:37:10 得分 0

long   java.util.Calendar.getTimeInMillis()   to   get   the   milliseconds   of   a   date   based   on   1970/1/1   00hrs00minutes00seconds  
   
  for   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   ideaTop

8 楼andyjt()回复于 2006-03-05 13:41:42 得分 0

core   java第四章上有Top

9 楼TONYBLARED(奔放的犀牛)回复于 2006-03-05 19:46:26 得分 0

/*  
    *   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);  
  }  
  }Top

10 楼sportboys(多动男孩)回复于 2006-03-05 19:57:37 得分 0

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

11 楼zhao_417(霍金)回复于 2006-03-06 09:51:47 得分 0

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

12 楼wanchengpeng(一米鸟)回复于 2006-03-06 10:26:44 得分 0

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

13 楼cxz7531(大花猫)回复于 2006-03-06 11:00:34 得分 0

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

14 楼crazy_he(天煞孤星)回复于 2006-03-06 12:49:32 得分 0

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

15 楼sportboys(多动男孩)回复于 2006-03-06 14:28:57 得分 0

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.Top

16 楼zhao_417(霍金)回复于 2006-03-06 14:53:46 得分 0

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);    
  }  
  }Top

17 楼zhao_417(霍金)回复于 2006-03-06 14:55:06 得分 0

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

18 楼cxz7531(大花猫)回复于 2006-03-06 15:48:48 得分 0

我前面已经给出答案了,绝对没有问题,写成个方法吧  
  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();  
          }  
  }Top

19 楼TONYBLARED(奔放的犀牛)回复于 2006-03-06 16:09:30 得分 0

大花猫的方法值得学习.  
  请允许我擅自修改一下:  
  Calendar   c01   =   Calendar.getInstance();  
  c01.set(year,   month,   0);  
   
  int   dayOfM   =   c01.get(Calendar.DAY_OF_MONTH);  
  System.out.println(year   +   "年"   +   month   +   "月有"   +   dayOfM   +   "天");  
   
  Top

20 楼zhao_417(霍金)回复于 2006-03-06 16:30:45 得分 0

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

21 楼martincsy(江湖肥虾米)回复于 2006-03-06 22:06:10 得分 0

提出另外一种思考方法。  
  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));  
  }Top

22 楼martincsy(江湖肥虾米)回复于 2006-03-06 22:18:10 得分 0

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

23 楼xsyl_1982(黑欲浪子)回复于 2006-03-06 22:30:55 得分 0

如果自己写   可以设一个数组   包括一年各个月的天数,然后判断这一年2月的天数,其他值可以直接从数组里取Top

24 楼sportboys(多动男孩)回复于 2006-03-07 18:38:00 得分 0

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

25 楼zhonghua2003(爱我中华)回复于 2006-03-09 19:34:22 得分 0

如果是1,3,5,7,8,10,12月的话就31天,  
   
  如果是4,6,9,11的话30天,  
   
  如果是润年2月28天,其他29天.Top

相关问题

  • 急.如何得到指定的一个月一共有多少天??
  • 如何知道某年某月一共有几天?
  • 怎么输出某年某月总共有多少天?
  • 怎样得到一个 .TXT 文件共有多少行 ?
  • 如何得到枚举类型共有多少个值?
  • 请问如何得到某月某日 是星期几
  • 如何寫一個函數得到某月最后一天?
  • ●在ASP中如何得到某月的最大天数?
  • 关于在List Control中得到它一共有几列的问题。
  • 谁有“根据某年某月得到这个月1号是星期几的源码函数”

关键词

  • date
  • calendar
  • 天数
  • month
  • c01
  • getactualmaximum
  • cl
  • 月
  • 得到
  • days

得分解答快速导航

  • 帖主:tanghulu
  • lydvqq

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo