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);
weihy(skr) ( ) 的解法,可以给些注解吗?
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
请仔细查看Calendar类的API Doc: month - the value used to set the MONTH time field. Month value is 0-based. e.g., 0 for January. 月是从0开始的!希望楼主能都看看文档!
也可以用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类会自动处理闰年的问题
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
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)+"日");
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;
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
对日期部分不太懂,痛苦啊
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);
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
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
month - the value used to set the MONTH time field. Month value is 0-based. e.g., 0 for January.
月是从0开始的!希望楼主能都看看文档!
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类会自动处理闰年的问题
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
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)+"日");
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;
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