如何得到某年某月共有多少天?急
请问,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




