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

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

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

    jojo's blog--快樂憂傷都與你同在
    為夢想而來,為自由而生。 性情若水,風起水興,風息水止,故時而激蕩,時又清平……
    posts - 11,  comments - 30,  trackbacks - 0

    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);
      
     }

    }
    posted on 2009-06-01 11:10 Blog of JoJo 閱讀(717) 評論(0)  編輯  收藏 所屬分類: Linux 技術相關Java 相關

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    常用鏈接

    留言簿(6)

    隨筆檔案

    文章分類

    文章檔案

    新聞分類

    新聞檔案

    相冊

    收藏夾

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲精品色婷婷在线影院| 日韩精品人妻系列无码专区免费 | 18女人毛片水真多免费| 亚洲一区二区三区AV无码 | MM131亚洲国产美女久久| 国产免费网站看v片在线| 中文字幕亚洲精品| 99久久久国产精品免费牛牛四川| 高潮毛片无遮挡高清免费视频| 久久久久亚洲AV成人网| 95老司机免费福利| 亚洲国产AV无码一区二区三区| 国产一级淫片a免费播放口之 | 午夜国产大片免费观看| 67194熟妇在线永久免费观看| 亚洲精品美女久久7777777| 亚洲网站在线免费观看| 免费高清小黄站在线观看| 一级女性全黄久久生活片免费 | 国产免费内射又粗又爽密桃视频| 亚洲中文字幕无码永久在线| ww4545四虎永久免费地址| 色妞www精品视频免费看| 亚洲国产精华液2020| 亚洲AV无码片一区二区三区| 中文字幕亚洲乱码熟女一区二区 | 国产精品无码亚洲一区二区三区| 国产A在亚洲线播放| 野花高清在线电影观看免费视频 | 少妇高潮太爽了在线观看免费| 永久在线免费观看| 在线观看免费中文视频| 1000部免费啪啪十八未年禁止观看 | 免费无码黄网站在线看| 国产真人无码作爱免费视频| 亚洲色偷偷综合亚洲AV伊人蜜桃 | 亚洲精品无码成人片久久不卡 | 亚洲一区二区免费视频| aⅴ在线免费观看| 和日本免费不卡在线v| 国产亚洲精品免费视频播放|