我要写一个界面,需要根据时间生成表格的列数,比如今天是“20090904”,则表格显示“20090901”,“20090902”,“20090903”,“20090904”
若为“20090905”,则表格显示“20090901”,“20090902”,“20090903”,“20090904”,“20090905”几列,以此类推,请问各位怎么实现?
若为“20090905”,则表格显示“20090901”,“20090902”,“20090903”,“20090904”,“20090905”几列,以此类推,请问各位怎么实现?
import java.util.*;
import java.text.*; public class DisplayDateEveryday {
public static void main(String args[]){
display("2008-08-08", "2008-08-24");
}
public static void display(String dateFirst, String dateSecond){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try{
Date dateOne = dateFormat.parse(dateFirst);
Date dateTwo = dateFormat.parse(dateSecond);
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateOne);
while(calendar.getTime().before(dateTwo)){
System.out.println(dateFormat.format(calendar.getTime()));
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/justinavril/archive/2008/09/06/2891266.aspx
先: SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMM01");
int i = Integer.parseInt(sdf.format(new Date()));
int j = Integer.parseInt(sdf2.format(new Date()));
然后再循环。具体的楼主
求出这两个日期的天数之差,然后再作相关处理:这里有个求天数的方法:
public static int getDays(Date d)
{
Calendar c = Calendar.getInstance();
c.setTime(d);
int dDay = c.get(Calendar.DATE);
int dYear = c.get(Calendar.YEAR);
int dMonth = c.get(Calendar.MONTH) + 1; //注意month是从0开始计的我们加1
int days=0;
int[] months={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(dYear%400==0||dYear%4==0&&dYear%100!=0){
months[2]=29;
}
days=(dYear-1)*365+(dYear-1)/4-(dYear-1)/100+(dYear-1)/400;
for(int i=1;i<dMonth;i++){
days+=months[i];
}
days+=dDay;
return days;
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yanliang_xt/archive/2009/09/03/4516579.aspx
具体在实现上还要作其它的考虑。
public static void main(String args[]){
display("2008-08-08", "2008-08-24");
}
public static void display(String dateFirst, String dateSecond){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try{
Date dateOne = dateFormat.parse(dateFirst);
Date dateTwo = dateFormat.parse(dateSecond);
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateOne);
while(calendar.getTime().before(dateTwo)){
System.out.println(dateFormat.format(calendar.getTime()));
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
}
catch(Exception e){
e.printStackTrace();
}
}
}