廉頗老矣,尚能飯否

          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;
             }
             
             //循環(huán)每個月
             //如果在日期范圍內月份循環(huán)時自增到了一年的最后一個月就將月份初始化到一月份
             int y = 0;
             //如果是開始日期的第一個年的月數就從開始月數循環(huán)
             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 + "月");
              
              //循環(huán)每一天
              int z = 1;
              //如果是開始日期的第一個月的天數就從開始天數循環(huán)
              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;
               }
              }


              //如果已經遍歷過了截至日期的最后月份就中止月份的循環(huán)
              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 閱讀(4999) 評論(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 類,不要重新發(fā)明輪子!  回復  更多評論   

          主站蜘蛛池模板: 松江区| 长乐市| 池州市| 交城县| 扶风县| 邛崃市| 赫章县| 罗江县| 聊城市| 漠河县| 麟游县| 梁平县| 岑溪市| 龙游县| 施甸县| 玉环县| 都匀市| 邢台市| 韶山市| 伊宁县| 泰宁县| 漳州市| 汶川县| 大荔县| 福海县| 铁岭县| 平湖市| 梓潼县| 江口县| 会同县| 平南县| 始兴县| 文山县| 贵州省| 揭阳市| 信丰县| 涞水县| 应用必备| 周至县| 嘉祥县| 隆化县|