廉颇老矣,尚能饭否

java:从技术到管理

常用链接

统计

最新评论

遍历两个日期之间天数的算法

package pkg.chart;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Test {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Long startM = sdf.parse("2009-1-14").getTime();
Long endM = sdf.parse("2010-1-14").getTime();
long result = (endM - startM) / (24 * 60 * 60 * 1000);
System.out.println("差:" + result + "天");

Date startDate = sdf.parse("2009-01-14");
Calendar startTime = Calendar.getInstance();
startTime.clear();
startTime.setTime(startDate);
for (int i = 0; i < (int)result;i++) {
String str = startTime.get(Calendar.YEAR) + "-"
+ startTime.get(Calendar.MONTH) + "-"
+ startTime.get(Calendar.DAY_OF_MONTH);
System.out.println(str);
startTime.add(Calendar.DAY_OF_YEAR, 1);
}
}
}


package demo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 遍历两个日期之间天数的算法
 *
 */
public class MyTest {
 public static void main(String[] args) throws ParseException {
  String start = "2007-01-27";
  String end = "2008-03-04";
  //字符串转换成日期
  SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
  Date startDate=format.parse(start);
  Calendar startTime=Calendar.getInstance();
  startTime.clear();
  startTime.setTime(startDate);
  int startYear = startTime.get(Calendar.YEAR);
  int startMonth = startTime.get(Calendar.MONTH);
  int startDay = startTime.get(Calendar.DAY_OF_MONTH);
  Date endDate=format.parse(end);
  Calendar endTime=Calendar.getInstance();
  endTime.clear();
  endTime.setTime(endDate);
  int endYear = endTime.get(Calendar.YEAR);
  int endMonth = endTime.get(Calendar.MONTH);
  int endDay = endTime.get(Calendar.DAY_OF_MONTH);
  System.out.println("注意西方的月份从0到11,中国的月份从1到12");
  System.out.println("下面输入的是中国的日期.注意其中的转换问题");
  System.out.println("start date : " + start);
  System.out.println("end date : " + end);
  
  int count = 0;
  for (int x = startYear; x <= endYear; x++) {
   //罗马历法产生年份公元1582年
   int gregorianCutoverYear = 1582;
   boolean isLeapYear = x >= gregorianCutoverYear ?
     ((x%4 == 0) && ((x%100 != 0) || (x%400 == 0))) :
      (x%4 == 0);
   //判断是否是闰年
   //java方法
   //boolean isLeapYear = (new GregorianCalendar()).isLeapYear(x);
   
   String isBigYear = "是平年";
   if (isLeapYear) {
    isBigYear = "是闰年";
   }
   System.out.println(x + "年" + isBigYear);
   
   //获取开始月的最大天数
   //java方法
   //SimpleDateFormat aFormat=new SimpleDateFormat("yyyy-MM-dd");
   //Date date = aFormat.parse(start);
   //Calendar time = Calendar.getInstance();
   //time.clear();
   //time.setTime(date);
   //int max=time.getActualMaximum(Calendar.DAY_OF_MONTH);//本月份的天数
   //System.out.println(max);
   
   //获取开始月的最大天数;大月是1,3,5,7,8,10,12;小月是4,6,9,11;特殊月是2
   int max = 0;
   if (startMonth == 1) {
    if (isLeapYear) {
     max = 29;
    }
    if (!isLeapYear) {
     max = 28;
    }
   }
   if (startMonth == 3 || startMonth == 5 || startMonth == 8 || startMonth == 10) {
    max = 30;
   }
   if (startMonth == 0 || startMonth == 2 || startMonth == 4 || startMonth == 6 || startMonth == 7 || startMonth == 9 || startMonth == 11) {
    max = 31;
   }
   
   //循环每个月
   //如果在日期范围内月份循环时自增到了一年的最后一个月就将月份初始化到一月份
   int y = 0;
   //如果是开始日期的第一个年的月数就从开始月数循环
   if (x == startYear) {
    y = startMonth;
   }
   for (; y < 12; y++) { 
    
    //获取当月的最大天数;大月是1,3,5,7,8,10,12;小月是4,6,9,11;特殊月是2
    max = 0;
    if (y == 1) {
     if (isLeapYear) {
      max = 29;
     }
     if (!isLeapYear) {
      max = 28;
     }
    }
    if (y == 3 || y == 5 || y == 8 || y == 10) {
     max = 30;
    }
    if (y == 0 || y == 2 || y == 4 || y == 6 || y == 7 || y == 9 || y == 11) {
     max = 31;
    }
    
    int ty = y + 1;
    System.out.println(x + "年" + ty + "月");
    
    //循环每一天
    int z = 1;
    //如果是开始日期的第一个月的天数就从开始天数循环
    if (x == startYear && y == startMonth) {
     z = startDay;
    }
    for (; z <= max; z++) {
     count++;
     
     System.out.println( x + "年" + ty + "月" + z + "日"); 
     
     if (x == endYear && y == endMonth && z == endDay) {
      break;
     }
    }


    //如果已经遍历过了截至日期的最后月份就中止月份的循环
    if (x == endYear && y == endMonth) {
     break;
    }
   
   }
  }
  
  System.out.println(start + " 到 " + end + " 的天数差:" + count);
  
 }

}



柳德才
13691193654
18942949207
QQ:422157370
liudecai_zan@126.com
湖北-武汉-江夏-庙山

posted on 2009-01-14 11:10 liudecai_zan@126.com 阅读(4981) 评论(10)  编辑  收藏 所属分类: 在路上

评论

# re: 遍历两个日期之间天数的算法 2009-01-14 12:04 altchen

public static void main(String [] args) throws ParseException{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Long startM=sdf.parse("2009-1-14").getTime();
Long endM=sdf.parse("2010-1-14").getTime();
long result = (endM-startM) / (24 * 60 * 60*1000);
System.out.println("差:"+result+"天");
}  回复  更多评论   

# re: 遍历两个日期之间天数的算法[未登录] 2009-01-14 12:27 Vincent

楼上是正解  回复  更多评论   

# re: 遍历两个日期之间天数的算法 2009-01-14 15:21 匿名

是啊,多简单的事,搞那么复杂干吗  回复  更多评论   

# re: 遍历两个日期之间天数的算法 2009-01-14 22:35 liudecai_zan@126.com

关键是遍历,因为我要用JFreechart做折线图,要用天做y轴的单位。于是顺便做了这个,并不是仅仅为了计算天数差。同时大家也可以看看我的代码,并不是排斥现成的,只是体现一种算法。同时谢谢大家。我的话比较直,只将技术,希望不会造成误解。  回复  更多评论   

# re: 遍历两个日期之间天数的算法 2009-01-15 11:21 altchen

博主研究技能的精神不錯.提個意見.如果要遍歷最好是用calendar.add(Calendar.DAY_OF_YEAR,1);就行了.jdk已經幫你考慮了潤年及月份天數的問題了.不用再自己判斷  回复  更多评论   

# re: 遍历两个日期之间天数的算法 2009-01-15 17:33 liudecai_zan@126.com

谢谢,这个方法我还真不知道  回复  更多评论   

# re: 遍历两个日期之间天数的算法 2009-01-15 17:52 liudecai_zan@126.com

综合大家的观点,觉得自己在写东西之前还是应该好好考虑一下是不是别人,包括sun的jdk等已经有了比较好的成熟的方式方法来解决特定的一个问题,免得自己误导了大家。下面是综合大家的代码形成的
package pkg.chart;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Test {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Long startM = sdf.parse("2009-1-14").getTime();
Long endM = sdf.parse("2010-1-14").getTime();
long result = (endM - startM) / (24 * 60 * 60 * 1000);
System.out.println("差:" + result + "天");

Date startDate = sdf.parse("2009-01-14");
Calendar startTime = Calendar.getInstance();
startTime.clear();
startTime.setTime(startDate);
for (int i = 0; i < (int)result;i++) {
String str = startTime.get(Calendar.YEAR) + "-"
+ startTime.get(Calendar.MONTH) + "-"
+ startTime.get(Calendar.DAY_OF_MONTH);
System.out.println(str);
startTime.add(Calendar.DAY_OF_YEAR, 1);
}
}
}  回复  更多评论   

# re: 遍历两个日期之间天数的算法[未登录] 2009-01-18 07:01 stanleyxu2005

先转换成utc时间,然后相减,再把time span转换回天数  回复  更多评论   

# re: 遍历两个日期之间天数的算法 2009-01-19 11:24 rapin

-.-有这么复杂吗?
遍历是相当的占资源的。  回复  更多评论   

# re: 遍历两个日期之间天数的算法 2009-01-19 12:30 娃娃

建议波主仔细学习calendar 类,不要重新发明轮子!  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: