廉頗老矣,尚能飯否

          java:從技術(shù)到管理

          常用鏈接

          統(tǒng)計(jì)

          最新評(píng)論

          遍歷兩個(gè)日期之間天數(shù)的算法

          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;

          /**
           * 遍歷兩個(gè)日期之間天數(shù)的算法
           *
           */
          public class MyTest {
           public static void main(String[] args) throws ParseException {
            String start = "2007-01-27";
            String end = "2008-03-04";
            //字符串轉(zhuǎn)換成日期
            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,中國(guó)的月份從1到12");
            System.out.println("下面輸入的是中國(guó)的日期.注意其中的轉(zhuǎn)換問(wèn)題");
            System.out.println("start date : " + start);
            System.out.println("end date : " + end);
            
            int count = 0;
            for (int x = startYear; x <= endYear; x++) {
             //羅馬歷法產(chǎn)生年份公元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);
             
             //獲取開(kāi)始月的最大天數(shù)
             //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);//本月份的天數(shù)
             //System.out.println(max);
             
             //獲取開(kāi)始月的最大天數(shù);大月是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)每個(gè)月
             //如果在日期范圍內(nèi)月份循環(huán)時(shí)自增到了一年的最后一個(gè)月就將月份初始化到一月份
             int y = 0;
             //如果是開(kāi)始日期的第一個(gè)年的月數(shù)就從開(kāi)始月數(shù)循環(huán)
             if (x == startYear) {
              y = startMonth;
             }
             for (; y < 12; y++) { 
              
              //獲取當(dāng)月的最大天數(shù);大月是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;
              //如果是開(kāi)始日期的第一個(gè)月的天數(shù)就從開(kāi)始天數(shù)循環(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;
               }
              }


              //如果已經(jīng)遍歷過(guò)了截至日期的最后月份就中止月份的循環(huán)
              if (x == endYear && y == endMonth) {
               break;
              }
             
             }
            }
            
            System.out.println(start + " 到 " + end + " 的天數(shù)差:" + count);
            
           }

          }



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

          posted on 2009-01-14 11:10 liudecai_zan@126.com 閱讀(4999) 評(píng)論(10)  編輯  收藏 所屬分類: 在路上

          評(píng)論

          # re: 遍歷兩個(gè)日期之間天數(shù)的算法 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+"天");
          }  回復(fù)  更多評(píng)論   

          # re: 遍歷兩個(gè)日期之間天數(shù)的算法[未登錄](méi) 2009-01-14 12:27 Vincent

          樓上是正解  回復(fù)  更多評(píng)論   

          # re: 遍歷兩個(gè)日期之間天數(shù)的算法 2009-01-14 15:21 匿名

          是啊,多簡(jiǎn)單的事,搞那么復(fù)雜干嗎  回復(fù)  更多評(píng)論   

          # re: 遍歷兩個(gè)日期之間天數(shù)的算法 2009-01-14 22:35 liudecai_zan@126.com

          關(guān)鍵是遍歷,因?yàn)槲乙肑Freechart做折線圖,要用天做y軸的單位。于是順便做了這個(gè),并不是僅僅為了計(jì)算天數(shù)差。同時(shí)大家也可以看看我的代碼,并不是排斥現(xiàn)成的,只是體現(xiàn)一種算法。同時(shí)謝謝大家。我的話比較直,只將技術(shù),希望不會(huì)造成誤解。  回復(fù)  更多評(píng)論   

          # re: 遍歷兩個(gè)日期之間天數(shù)的算法 2009-01-15 11:21 altchen

          博主研究技能的精神不錯(cuò).提個(gè)意見(jiàn).如果要遍歷最好是用calendar.add(Calendar.DAY_OF_YEAR,1);就行了.jdk已經(jīng)幫你考慮了潤(rùn)年及月份天數(shù)的問(wèn)題了.不用再自己判斷  回復(fù)  更多評(píng)論   

          # re: 遍歷兩個(gè)日期之間天數(shù)的算法 2009-01-15 17:33 liudecai_zan@126.com

          謝謝,這個(gè)方法我還真不知道  回復(fù)  更多評(píng)論   

          # re: 遍歷兩個(gè)日期之間天數(shù)的算法 2009-01-15 17:52 liudecai_zan@126.com

          綜合大家的觀點(diǎn),覺(jué)得自己在寫東西之前還是應(yīng)該好好考慮一下是不是別人,包括sun的jdk等已經(jīng)有了比較好的成熟的方式方法來(lái)解決特定的一個(gè)問(wèn)題,免得自己誤導(dǎo)了大家。下面是綜合大家的代碼形成的
          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);
          }
          }
          }  回復(fù)  更多評(píng)論   

          # re: 遍歷兩個(gè)日期之間天數(shù)的算法[未登錄](méi) 2009-01-18 07:01 stanleyxu2005

          先轉(zhuǎn)換成utc時(shí)間,然后相減,再把time span轉(zhuǎn)換回天數(shù)  回復(fù)  更多評(píng)論   

          # re: 遍歷兩個(gè)日期之間天數(shù)的算法 2009-01-19 11:24 rapin

          -.-有這么復(fù)雜嗎?
          遍歷是相當(dāng)?shù)恼假Y源的。  回復(fù)  更多評(píng)論   

          # re: 遍歷兩個(gè)日期之間天數(shù)的算法 2009-01-19 12:30 娃娃

          建議波主仔細(xì)學(xué)習(xí)calendar 類,不要重新發(fā)明輪子!  回復(fù)  更多評(píng)論   

          主站蜘蛛池模板: 罗定市| 洱源县| 东海县| 宣城市| 龙里县| 昂仁县| 忻城县| 柯坪县| 广河县| 曲麻莱县| 常山县| 化德县| 贵州省| 清远市| 柳河县| 水城县| 浦东新区| 绍兴县| 新津县| 通渭县| 望城县| 古浪县| 昌乐县| 奎屯市| 山阴县| 上蔡县| 盐亭县| 安岳县| 丰顺县| 皮山县| 馆陶县| 宜兰县| 延安市| 夏河县| 商洛市| 临沭县| 泰州市| 乌鲁木齐市| 承德县| 乐至县| 金湖县|