<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    廉頗老矣,尚能飯否

    java:從技術到管理

    常用鏈接

    統(tǒng)計

    最新評論

    遍歷兩個日期之間天數(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;

    /**
     * 遍歷兩個日期之間天數(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,中國的月份從1到12");
      System.out.println("下面輸入的是中國的日期.注意其中的轉(zhuǎ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);
       
       //獲取開始月的最大天數(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);
       
       //獲取開始月的最大天數(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)每個月
       //如果在日期范圍內(nèi)月份循環(huán)時自增到了一年的最后一個月就將月份初始化到一月份
       int y = 0;
       //如果是開始日期的第一個年的月數(shù)就從開始月數(shù)循環(huán)
       if (x == startYear) {
        y = startMonth;
       }
       for (; y < 12; y++) { 
        
        //獲取當月的最大天數(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;
        //如果是開始日期的第一個月的天數(shù)就從開始天數(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)遍歷過了截至日期的最后月份就中止月份的循環(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 閱讀(4997) 評論(10)  編輯  收藏 所屬分類: 在路上

    評論

    # re: 遍歷兩個日期之間天數(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+"天");
    }  回復  更多評論   

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

    樓上是正解  回復  更多評論   

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

    是啊,多簡單的事,搞那么復雜干嗎  回復  更多評論   

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

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

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

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

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

    謝謝,這個方法我還真不知道  回復  更多評論   

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

    綜合大家的觀點,覺得自己在寫東西之前還是應該好好考慮一下是不是別人,包括sun的jdk等已經(jīng)有了比較好的成熟的方式方法來解決特定的一個問題,免得自己誤導了大家。下面是綜合大家的代碼形成的
    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: 遍歷兩個日期之間天數(shù)的算法[未登錄] 2009-01-18 07:01 stanleyxu2005

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

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

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

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

    建議波主仔細學習calendar 類,不要重新發(fā)明輪子!  回復  更多評論   

    主站蜘蛛池模板: 亚洲乱码国产乱码精品精| 免费在线观看a级毛片| 亚洲一区综合在线播放| 一级特级aaaa毛片免费观看| 国产成人在线免费观看| 亚洲精品V天堂中文字幕| 在线观看免费精品国产| 18禁亚洲深夜福利人口| 国产亚洲福利一区二区免费看| 亚洲av日韩综合一区久热| 国产精品免费看香蕉| 一本久久免费视频| 中文字幕亚洲一区二区va在线| 久久www免费人成精品香蕉| 亚洲VA中文字幕无码一二三区| 久久精品免费观看国产| 亚洲免费二区三区| 夜夜爽免费888视频| 黄网站在线播放视频免费观看| 亚洲女同成人AⅤ人片在线观看| 中文字幕免费在线播放| 内射干少妇亚洲69XXX| aa级一级天堂片免费观看| 美女露100%胸无遮挡免费观看| 亚洲色图综合在线| 免费人成视频在线观看网站| 亚洲欧洲日韩极速播放| 男人的天堂亚洲一区二区三区| 国产AV无码专区亚洲AV麻豆丫| 亚洲一级片内射网站在线观看| 国产亚洲免费的视频看| 亚洲不卡在线观看| 亚洲午夜AV无码专区在线播放| 人人玩人人添人人澡免费| 中文字幕 亚洲 有码 在线| 亚洲精品动漫人成3d在线| 99久在线国内在线播放免费观看 | 一级毛片在线免费看| 亚洲国产成a人v在线观看 | 国产精品va无码免费麻豆| 免费国产污网站在线观看|