一个关于日期的问题,请各位DX不吝啬赐教!!!!

llajjqq 2004-03-29 08:40:20
条件:给出两个日期 ,如 2003.02.11 和 2003.05.01; 日期是可以任意的,请大家考虑闰年的问题。
结论:列出两个日期间的所有天,如:2003.02.11 、 2003.02.12 ... 2003.05.01

请各位DX帮帮忙!
...全文
43 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
satangf 2004-03-30
  • 打赏
  • 举报
回复
不错!
goodsong 2004-03-30
  • 打赏
  • 举报
回复
Before learn anything, make sure you are ready to learn,
or you'll learn nothing

也可以用Date类实现:
Date date1 = new Date(104, 1, 11); //2004年2月11日
Date date2 = new Date(104, 4, 11); //2004年5月11日
long x=date1.getTime();
long y=date2.getTime();
for(long i=x;i<=y;i=i+24*60*60*1000) {
Date dateTemp=new Date(i);
System.out.println(dateTemp); //所有的天
}

Data类会自动处理闰年的问题

if above snippet could get the correct result,will you copy it?
Please see the API documents and decided by yourself
qwe0828 2004-03-30
  • 打赏
  • 举报
回复
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = format.parse(beforDate);
Date date2 = format.parse(afterDate);
long decrease = (Util.getDateBetween(date1,date2))/1000/3600/24;
int dateDiff = (int)decrease;
return dateDiff;
goodsong 2004-03-30
  • 打赏
  • 举报
回复
public final void set(int year,
int month,
int date)Sets the values for the fields year, month, and date. Previous values of other fields are retained. If this is not desired, call clear first.

Parameters:
year - the value used to set the YEAR time field.
>>month - the value used to set the MONTH time field. Month value is 0-based. e.g., 0 for January.
date - the value used to set the DATE time field.

above is what danceflash(Wine) means


if you want to set the date 2003-2-11,you should write
cl.set(2003,1,11) and also
System.out.println(c1.get(Calendar.YEAR)+"年"
+(c1.get(Calendar.MONTH)+1)+"月"
+c1.get(Calendar.DATE)+"日");
goodsong 2004-03-30
  • 打赏
  • 举报
回复
what I have said was "above is what weihy(skr)'s snippet means"
I feel so sorry I only used five APIs and you didn't read the API documents carefully yourself.
I'm a little happy that the result is not correct
If you got the correct answer, you just learned Ctrl+C and Ctrl+V
This won't help you to be an excellent man
Raulgodle 2004-03-30
  • 打赏
  • 举报
回复
hao tie
runki 2004-03-30
  • 打赏
  • 举报
回复
同意Danger2000(飞鱼)
llajjqq 2004-03-30
  • 打赏
  • 举报
回复
谢谢各位 !!!!
goodsong 2004-03-29
  • 打赏
  • 举报
回复
Calendar c= Calendar.getInstance();
c.set(2004, 4,1);
/*
* Date Arithmetic function.
* Adds the specified (signed) amount of time to the given time field,
* based on the calendar's rules.
*/
c.add(Calendar.DATE, 1);
for your problem,you can write
void listDays()
{
Calendar c1= Calendar.getInstance();
Calendar c2= Calendar.getInstance();
c1.set(2003, 2,11);
c2.set(2003, 5,1);
while(!c1.equals(c2))
{
c1.add(Calendar.DATE, 1);
System.out.println(c1.get(Calendar.YEAR).toString()
+c1.get(Calendar.MONTH).toString()
+c1.get(Calendar.DATE).toString());
}
}
above is what weihy(skr)'s snippet means
llajjqq 2004-03-29
  • 打赏
  • 举报
回复
weihy(skr) ( ) 的解法,可以给些注解吗?
ddadoris 2004-03-29
  • 打赏
  • 举报
回复
up
weihy 2004-03-29
  • 打赏
  • 举报
回复
Calendar c= Calendar.getInstance();
c.set(2004, 4,1);
/*
* Date Arithmetic function.
* Adds the specified (signed) amount of time to the given time field,
* based on the calendar's rules.
*/
c.add(Calendar.DATE, 1);
llajjqq 2004-03-29
  • 打赏
  • 举报
回复
hashtable 和 vector 在 java2 里已经不推荐使用了
llajjqq 2004-03-29
  • 打赏
  • 举报
回复
java里面已经给你实现得很好乐
对日期部分不太懂,痛苦啊。。。。。。
llajjqq 2004-03-29
  • 打赏
  • 举报
回复
有没有具体的java api 可以直接调用啊
zhang21cnboy 2004-03-29
  • 打赏
  • 举报
回复
java里面已经给你实现得很好乐
yangjuanli 2004-03-29
  • 打赏
  • 举报
回复
闰年也只有2月是29天呀。。。对于其他月份是不变的呀。。。。
用数组等都可以完全或者VECTOR或者HASHTABLE就可以做出来,
仔细想想,应该很简单的。。。
Danger2000 2004-03-29
  • 打赏
  • 举报
回复
也可以用Date类实现:
Date date1 = new Date(104, 1, 11); //2004年2月11日
Date date2 = new Date(104, 4, 11); //2004年5月11日
long x=date1.getTime();
long y=date2.getTime();
for(long i=x;i<=y;i=i+24*60*60*1000) {
Date dateTemp=new Date(i);
System.out.println(dateTemp); //所有的天
}

Data类会自动处理闰年的问题
danceflash 2004-03-29
  • 打赏
  • 举报
回复
请仔细查看Calendar类的API Doc:
month - the value used to set the MONTH time field. Month value is 0-based. e.g., 0 for January.
月是从0开始的!

希望楼主能都看看文档!
llajjqq 2004-03-29
  • 打赏
  • 举报
回复
goodsong,我把你的代码运行了一下,


import java.util.*;

class DateTest{
public static void main(String[] args){
Calendar c1= Calendar.getInstance();
Calendar c2= Calendar.getInstance();
c1.set(2003, 2,11);
c2.set(2003, 5,1);
while(!c1.equals(c2))
{
c1.add(Calendar.DATE, 1);
System.out.println(c1.get(Calendar.YEAR)+"年"
+c1.get(Calendar.MONTH)+"月"
+c1.get(Calendar.DATE)+"日");
}
}
}

发现2月竟然有31天,不知道是什么原因,盼给个解释!!!!1

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧