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 閱讀(719) 評論(0)  編輯  收藏 所屬分類: Linux 技術相關Java 相關

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(6)

          隨筆檔案

          文章分類

          文章檔案

          新聞分類

          新聞檔案

          相冊

          收藏夾

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 渑池县| 灵武市| 胶州市| 闽清县| 临沂市| 大埔区| 江西省| 子洲县| 太谷县| 望奎县| 枝江市| 汝阳县| 汉寿县| 共和县| 永川市| 阜康市| 福海县| 岳普湖县| 唐海县| 察哈| 津市市| 祁门县| 游戏| 广东省| 东乌珠穆沁旗| 武夷山市| 满洲里市| 永靖县| 平远县| 黄骅市| 浦北县| 宝清县| 府谷县| 苏尼特右旗| 嘉峪关市| 沈阳市| 婺源县| 神农架林区| 容城县| 丹阳市| 永春县|