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

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

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

    廉頗老矣,尚能飯否

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

    常用鏈接

    統(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++) { 
        
        //獲取當(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;
        //如果是開始日期的第一個月的天數(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 閱讀(4998) 評論(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+"天");
    }  回復(fù)  更多評論   

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

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

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

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

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

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

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

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

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

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

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

    綜合大家的觀點,覺得自己在寫東西之前還是應(yīng)該好好考慮一下是不是別人,包括sun的jdk等已經(jīng)有了比較好的成熟的方式方法來解決特定的一個問題,免得自己誤導(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ù)  更多評論   

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

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

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

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

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

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

    主站蜘蛛池模板: 日本亚洲欧美色视频在线播放 | 成全在线观看免费观看大全| 九九久久精品国产免费看小说| 婷婷亚洲综合一区二区| 亚洲国产精品精华液| 亚洲熟妇成人精品一区| 中文字幕在线观看亚洲日韩| jlzzjlzz亚洲jzjzjz| 91亚洲视频在线观看| 亚洲成a人片在线观看精品| 亚洲AV无码一区二区三区人| 亚洲最大福利视频| 亚洲另类无码专区首页| 婷婷国产偷v国产偷v亚洲| 免费一级做a爰片久久毛片潮| 成人福利在线观看免费视频| 一级特级女人18毛片免费视频| 一级白嫩美女毛片免费| 国产一级a毛一级a看免费视频| 少妇性饥渴无码A区免费| 久久免费看少妇高潮V片特黄| 亚洲最大免费视频网| 67194熟妇在线永久免费观看| 成人免费午夜在线观看| 免费A级毛片无码A∨男男| 激情97综合亚洲色婷婷五| 亚洲AV永久精品爱情岛论坛| 4480yy私人影院亚洲| 亚洲色大成网站www| 日本高清不卡中文字幕免费| 永久免费av无码网站yy| 亚洲精品视频免费看| 国产成人免费永久播放视频平台 | 日韩视频在线精品视频免费观看| 成人毛片免费观看视频在线| 国产精品美女自在线观看免费| 国产成人精品曰本亚洲79ren| 亚洲综合国产精品| 亚洲人成未满十八禁网站| 丰满妇女做a级毛片免费观看| 高清一区二区三区免费视频|