最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java實(shí)現(xiàn)日歷(某年的日歷,某月的日歷)用戶(hù)完全自定義

 更新時(shí)間:2013年05月02日 15:43:58   作者:  
本篇文章介紹了,java實(shí)現(xiàn)日歷(某年的日歷,某月的日歷)用戶(hù)完全自定義。需要的朋友參考下

用戶(hù)可以自定義打印某一年的年歷,即:把某一年的日歷全部打印出來(lái)

如把2013年的年歷打印出來(lái)如下:

復(fù)制代碼 代碼如下:

January  2013         
---------------------------------------------
   Sun   Mon   Tue   Wed   Thu   Fri   Sat
     2     3     4     5
     7     8     9     10    11    12
    14    15    16    17    18    19
    21    22    23    24    25    26
    28    29    30    31

             Febuary  2013         
---------------------------------------------
   Sun   Mon   Tue   Wed   Thu   Fri   Sat
     2
     4     5     6     7     8     9
    11    12    13    14    15    16
    18    19    20    21    22    23
    25    26    27    28

             March  2013         
---------------------------------------------
   Sun   Mon   Tue   Wed   Thu   Fri   Sat
     2
     4     5     6     7     8     9
    11    12    13    14    15    16
    18    19    20    21    22    23
    25    26    27    28    29    30


             April  2013         
---------------------------------------------
   Sun   Mon   Tue   Wed   Thu   Fri   Sat
     2     3     4     5     6
     8     9     10    11    12    13
    15    16    17    18    19    20
    22    23    24    25    26    27
    29    30

             May  2013         
---------------------------------------------
   Sun   Mon   Tue   Wed   Thu   Fri   Sat
     2     3     4
     6     7     8     9     10    11
    13    14    15    16    17    18
    20    21    22    23    24    25
    27    28    29    30    31

             Jun  2013         
---------------------------------------------
   Sun   Mon   Tue   Wed   Thu   Fri   Sat

     3     4     5     6     7     8
     10    11    12    13    14    15
    17    18    19    20    21    22
    24    25    26    27    28    29


             July  2013         
---------------------------------------------
   Sun   Mon   Tue   Wed   Thu   Fri   Sat
     2     3     4     5     6
     8     9     10    11    12    13
    15    16    17    18    19    20
    22    23    24    25    26    27
    29    30    31

             August  2013         
---------------------------------------------
   Sun   Mon   Tue   Wed   Thu   Fri   Sat
     2     3
     5     6     7     8     9     10
    12    13    14    15    16    17
    19    20    21    22    23    24
    26    27    28    29    30    31

             Septermber  2013         
---------------------------------------------
   Sun   Mon   Tue   Wed   Thu   Fri   Sat
     2     3     4     5     6     7
     9     10    11    12    13    14
    16    17    18    19    20    21
    23    24    25    26    27    28
    30

             October  2013         
---------------------------------------------
   Sun   Mon   Tue   Wed   Thu   Fri   Sat
     2     3     4     5
     7     8     9     10    11    12
    14    15    16    17    18    19
    21    22    23    24    25    26
    28    29    30    31

             November  2013         
---------------------------------------------
   Sun   Mon   Tue   Wed   Thu   Fri   Sat
     2
     4     5     6     7     8     9
    11    12    13    14    15    16
    18    19    20    21    22    23
    25    26    27    28    29    30

             December  2013         
---------------------------------------------
   Sun   Mon   Tue   Wed   Thu   Fri   Sat
     2     3     4     5     6     7
     9     10    11    12    13    14
    16    17    18    19    20    21
    23    24    25    26    27    28
    30    31


當(dāng)然用戶(hù)如果想單獨(dú)打印某個(gè)月的日歷,同樣是可以實(shí)現(xiàn)的

如打?。?014年1月份的日歷

復(fù)制代碼 代碼如下:

日    一    二    三    四    五    六
     2     3     4   
     6     7     8     9    10    11   
    13    14    15    16    17    18   
    20    21    22    23    24    25   
    27    28    29    30    31

用戶(hù)還可以實(shí)現(xiàn)打印當(dāng)前的月份的日歷

今天是:2013-04-27,則當(dāng)前月份的日歷打印如下:

復(fù)制代碼 代碼如下:

日    一    二    三    四    五    六
     2     3     4     5     6   
     8     9    10    11    12    13   
    15    16    17    18    19    20   
    22    23    24    25    26    :)27(:   
    29    30

是的,你沒(méi)有看錯(cuò),在27的那里有一個(gè)標(biāo)志,表示是當(dāng)天的日期.....

下面進(jìn)入代碼部分:

========================================================

代碼部分:

========================================================

/UUUUUU_Test/src/com/b510/date/HongtenDate.java

復(fù)制代碼 代碼如下:

/**
  *
  */
 package com.b510.date;

 import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.GregorianCalendar;

 /**
  * 一個(gè)日期處理類(lèi),在該類(lèi)中,構(gòu)造函數(shù)<code>HongtenDate()</code>,系統(tǒng)會(huì)默認(rèn)設(shè)置年份為當(dāng)年<br>
  * 而<code>HongtenDate(int year)</code>,則可以自定義年份<br>
  *
  * <pre>
  * HongtenDate date = new HongtenDate();
  * </pre>
  *
  * or<br>
  *
  * <pre>
  * HongtenDate hd = new HongtenDate(2014);
  * </pre>
  *
  * 調(diào)用<code>printCalendar()</code>可以打印一年的日期<br>
  * 調(diào)用<code>printCurrentMonth()</code>可以打印當(dāng)前月的日期<br>
  * ,當(dāng)然,你也可以調(diào)用<br>
  * <code>printMonthOfYear()</code>設(shè)置一個(gè)參數(shù),進(jìn)行打印某個(gè)月的日期<br>
  * 這里提供一些參考方案:<br>
  *
  * <pre>
  * // 無(wú)參數(shù),系統(tǒng)默認(rèn)去當(dāng)前年份
  * HongtenDate date = new HongtenDate();
  * date.printCalendar();
  * date.printCurrentMonth();
  * date.printMonthOfYear(4);
  * </pre>
  *
  * or<br>
  *
  * <pre>
  * // 設(shè)置為2014年
  * HongtenDate hd = new HongtenDate(2014);
  * hd.printCurrentMonth();
  * hd.printMonthOfYear(1);
  * </pre>
  *
  * @date 2013-4-27
  * @author hongten
  *
  */
 public class HongtenDate {

     // MONTHS
     // ============================================
     public static final String JANUARY = "January";
     public static final String FEBUARY = "Febuary";
     public static final String MARCH = "March";
     public static final String APRIL = "April";
     public static final String MAY = "May";
     public static final String JUN = "Jun";
     public static final String JULY = "July";
     public static final String AUGUST = "August";
     public static final String SEPTERMBER = "Septermber";
     public static final String OCTOBER = "October";
     public static final String NOVEMBER = "November";
     public static final String DECEMBER = "December";

     /**
      * 年份
      */
     private int year;
     /**
      * 一月一日星期幾(eg:2013-01-01-->星期二,所以<code>whatDayOnJanuaryOne = 2;</code>)
      */
     private int whatDayOnJanuaryOne;

     // main
     // ======================================
     public static void main(String[] args) throws Exception {
         // 無(wú)參數(shù),系統(tǒng)默認(rèn)去當(dāng)前年份
         HongtenDate date = new HongtenDate();
         // date.printCalendar();
         date.printCurrentMonth();
         // date.printMonthOfYear(4);

         // 設(shè)置為2014年
         HongtenDate hd = new HongtenDate(2014);
         // hd.printCurrentMonth();
         // hd.printMonthOfYear(1);

     }

     // 無(wú)參數(shù),系統(tǒng)默認(rèn)去當(dāng)前年份
     public HongtenDate() {
         Calendar cal = Calendar.getInstance();
         this.year = cal.get(Calendar.YEAR);
         try {
             setWhatDayOnJanuaryOne(getJanuaryOne(year));
         } catch (Exception e) {
             e.printStackTrace();
         }
     }

     // 有參數(shù),設(shè)置年份
     public HongtenDate(int year) {
         this.year = year;
         try {
             setWhatDayOnJanuaryOne(getJanuaryOne(year));
         } catch (Exception e) {
             e.printStackTrace();
         }
     }

     /**
      * 打印某個(gè)月的所有日期
      *
      * @param mon
      *            月份
      * @throws Exception
      */
     public void printMonthOfYear(int mon) throws Exception {
         if (mon < 1 || mon > 12) {
             System.out.println("你輸入的月份[" + mon + "]不對(duì),請(qǐng)檢查在進(jìn)行....");
             return;
         }
         GregorianCalendar now = new GregorianCalendar();
         // 獲得一個(gè)Date對(duì)象
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
         Date date = sdf.parse(year + "-" + mon + "-01");
         // 設(shè)置當(dāng)前時(shí)間
         now.setTime(date);
         // 從日期中取得當(dāng)前的月
         int month = now.get(Calendar.MONTH);
         // 設(shè)置now的日期為1
         now.set(Calendar.DAY_OF_MONTH, 1);
         // 得到now是一周的第幾天
         int week = now.get(Calendar.DAY_OF_WEEK);
         // 打印日歷頭部標(biāo)示
         System.out.println("日\(chéng)t一\t二\t三\t四\t五\t六");
         // 打印當(dāng)前日期前面的空格
         for (int i = Calendar.SUNDAY; i < week; i++) {
             System.out.print("\t");
         }
         // 打印日歷主體
         while (now.get(Calendar.MONTH) == month) {
             int day = now.get(Calendar.DAY_OF_MONTH);
             // 對(duì)輸出的日歷進(jìn)行對(duì)齊,小于10的加上一個(gè)空格
             if (day < 10) {
                 System.out.print(" " + day + "\t");
             } else {
                 System.out.print("" + day + "\t");
             }
             // 如果是周六,進(jìn)行換行
             if (week == Calendar.SATURDAY) {
                 System.out.println();
             }
             // 每次輸出日期后,將日期增加一天
             now.add(Calendar.DAY_OF_MONTH, 1);
             // 重新獲得一周的第幾天
             week = now.get(Calendar.DAY_OF_WEEK);
         }
     }

     /**
      * 打印當(dāng)前月的所有日期,這個(gè)不會(huì)因用戶(hù)設(shè)置的年份而改變
      */
     public void printCurrentMonth() {
         GregorianCalendar now = new GregorianCalendar();
         // 獲得一個(gè)Date對(duì)象
         Date date = new Date();
         // 設(shè)置當(dāng)前時(shí)間
         now.setTime(date);
         // 從日期中取得當(dāng)前的日
         int toDay = now.get(Calendar.DAY_OF_MONTH);
         // 從日期中取得當(dāng)前的月
         int month = now.get(Calendar.MONTH);
         // 設(shè)置now的日期為1
         now.set(Calendar.DAY_OF_MONTH, 1);
         // 得到now是一周的第幾天
         int week = now.get(Calendar.DAY_OF_WEEK);
         // 打印日歷頭部標(biāo)示
         System.out.println("日\(chéng)t一\t二\t三\t四\t五\t六");
         // 打印當(dāng)前日期前面的空格
         for (int i = Calendar.SUNDAY; i < week; i++) {
             System.out.print("\t");
         }
         // 打印日歷主體
         while (now.get(Calendar.MONTH) == month) {
             int day = now.get(Calendar.DAY_OF_MONTH);
             // 對(duì)輸出的日歷進(jìn)行對(duì)齊,小于10的加上一個(gè)空格
             if (day < 10) {
                 // 如果是當(dāng)前日期,加上標(biāo)示
                 if (day == toDay) {
                     System.out.print(":)" + day + "(:\t");
                 } else {
                     System.out.print(" " + day + "\t");
                 }
             } else {
                 // 如果是當(dāng)前日期,加上標(biāo)示
                 if (day == toDay) {
                     System.out.print(":)" + day + "(:\t");
                 } else {
                     System.out.print("" + day + "\t");
                 }
             }
             // 如果是周六,進(jìn)行換行
             if (week == Calendar.SATURDAY) {
                 System.out.println();
             }
             // 每次輸出日期后,將日期增加一天
             now.add(Calendar.DAY_OF_MONTH, 1);
             // 重新獲得一周的第幾天
             week = now.get(Calendar.DAY_OF_WEEK);
         }
     }

     /**
      * 獲取year這一年的一月一號(hào)是星期幾
      *
      * @param year
      *            年份
      * @return
      * @throws Exception
      */
     public int getJanuaryOne(int year) throws Exception {
         int[] weekDays = { 0, 1, 2, 3, 4, 5, 6 };
         Calendar cal = Calendar.getInstance();
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
         Date dt = sdf.parse(year + "-01-01");
         cal.setTime(dt);
         int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
         if (w < 0)
             w = 0;
         return weekDays[w];
     }

     /**
      * 打印一年的所有月份
      */
     public void printCalendar() {
         for (int i = 1; i <= 12; i++) {
             String month = getMonth(i);
             printTitle(month);
             // 打印有31天的月份
             if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) {
                 print31();
             }
             // 打印有30天的月份
             else if (i == 4 || i == 6 || i == 9 || i == 11) {
                 print30();
             }
             // 打印二月份
             else if (i == 2) {
                 printFebuary();
             }
             System.out.println();
         }
     }

     // 打印格式
     // ============================================== start
     /**
      * 打印二月份,每一年的二月份可能不相同,所以要單獨(dú)處理
      */
     protected void printFebuary() {
         if (this.year % 4 == 0) {
             // 閏年
             printLeapYear();
         } else {
             // 平年
             printNonleapYear();
         }
     }

     /**
      * 閏年二月份打印格式
      */
     private void printLeapYear() {
         for (int j = 1; j <= 29; j++) {
             String tmp = "";
             if (j == 1) {
                 for (int k = 1; k <= this.whatDayOnJanuaryOne % 7; k++) {
                     tmp = tmp + "      ";
                 }
                 tmp = tmp + "   " + j + "  ";
                 if (this.whatDayOnJanuaryOne == 6) {
                     System.out.println(tmp);
                 } else {
                     System.out.print(tmp);
                 }
             } else if (j > 1 && j < 29) {

                 if ((this.whatDayOnJanuaryOne + j) % 7 == 0) {
                     System.out.println("   " + j);
                 } else {
                     if (j < 10) {
                         System.out.print("   " + j + "  ");
                     } else {
                         System.out.print("   " + j + " ");
                     }
                 }
             } else if (j == 29) {
                 System.out.println("   " + j);
                 this.whatDayOnJanuaryOne = ((this.whatDayOnJanuaryOne + j) % 7);
             }
         }
     }

     /**
      * 打印平年二月份格式
      */
     private void printNonleapYear() {
         for (int j = 1; j <= 28; j++) {
             String tmp = "";
             if (j == 1) {
                 for (int k = 1; k <= this.whatDayOnJanuaryOne % 7; k++) {
                     tmp = tmp + "      ";
                 }
                 tmp = tmp + "   " + j + "  ";
                 if (this.whatDayOnJanuaryOne == 6) {
                     System.out.println(tmp);
                 } else {
                     System.out.print(tmp);
                 }
             } else if (j > 1 && j < 28) {

                 if ((this.whatDayOnJanuaryOne + j) % 7 == 0) {
                     System.out.println("   " + j);
                 } else {
                     if (j < 10) {
                         System.out.print("   " + j + "  ");
                     } else {
                         System.out.print("   " + j + " ");
                     }
                 }
             } else if (j == 28) {
                 System.out.println("   " + j);
                 this.whatDayOnJanuaryOne = ((this.whatDayOnJanuaryOne + j) % 7);
             }
         }
     }

     /**
      * 打印有30天的月份
      */
     protected void print30() {
         for (int j = 1; j <= 30; j++) {
             String tmp = "";
             if (j == 1) {
                 for (int k = 1; k <= this.whatDayOnJanuaryOne % 7; k++) {
                     tmp = tmp + "      ";
                 }
                 tmp = tmp + "   " + j + "  ";
                 if (this.whatDayOnJanuaryOne == 6) {
                     System.out.println(tmp);
                 } else {
                     System.out.print(tmp);
                 }
             } else if (j > 1 && j < 30) {

                 if ((this.whatDayOnJanuaryOne + j) % 7 == 0) {
                     System.out.println("   " + j);
                 } else {
                     if (j < 10) {
                         System.out.print("   " + j + "  ");
                     } else {
                         System.out.print("   " + j + " ");
                     }
                 }
             } else if (j == 30) {
                 System.out.println("   " + j);
                 this.whatDayOnJanuaryOne = ((this.whatDayOnJanuaryOne + j) % 7);
             }
         }
     }

     /**
      * 打印有31天的月份
      */
     protected void print31() {
         for (int j = 1; j <= 31; j++) {
             String tmp = "";
             if (j == 1) {
                 for (int k = 1; k <= this.whatDayOnJanuaryOne % 7; k++) {
                     tmp = tmp + "      ";
                 }
                 tmp = tmp + "   " + j + "  ";
                 if (this.whatDayOnJanuaryOne == 6) {
                     System.out.println(tmp);
                 } else {
                     System.out.print(tmp);
                 }
             } else if (j > 1 && j < 31) {

                 if ((this.whatDayOnJanuaryOne + j) % 7 == 0) {
                     System.out.println("   " + j);
                 } else {
                     if (j < 10) {
                         System.out.print("   " + j + "  ");
                     } else {
                         System.out.print("   " + j + " ");
                     }
                 }
             } else if (j == 31) {
                 System.out.println("   " + j);
                 this.whatDayOnJanuaryOne = ((this.whatDayOnJanuaryOne + j) % 7);
             }
         }
     }

     /**
      * 打印每個(gè)月的標(biāo)題
      *
      * @param month
      */
     protected void printTitle(String month) {
         System.out.println("             " + month + "  " + this.year + "          ");
         System.out.println("---------------------------------------------");
         System.out.println("   Sun   Mon   Tue   Wed   Thu   Fri   Sat");
     }

     // 打印格式
     // ============================================== end

     /**
      * 獲取月份的英文名稱(chēng)
      *
      * @param m
      *            月份的數(shù)字表示
      * @return
      */
     public String getMonth(int m) {
         String month = "";
         switch (m) {
         case 1:
             month = JANUARY;
             break;
         case 2:
             month = FEBUARY;
             break;
         case 3:
             month = MARCH;
             break;
         case 4:
             month = APRIL;
             break;
         case 5:
             month = MAY;
             break;
         case 6:
             month = JUN;
             break;
         case 7:
             month = JULY;
             break;
         case 8:
             month = AUGUST;
             break;
         case 9:
             month = SEPTERMBER;
             break;
         case 10:
             month = OCTOBER;
             break;
         case 11:
             month = NOVEMBER;
             break;
         case 12:
             month = DECEMBER;
             break;
         }
         return month;
     }

     public int getYear() {
         return year;
     }

     public void setYear(int year) {
         this.year = year;
     }

     public int getWhatDayOnJanuaryOne() {
         return whatDayOnJanuaryOne;
     }

     public void setWhatDayOnJanuaryOne(int whatDayOnJanuaryOne) {
         this.whatDayOnJanuaryOne = whatDayOnJanuaryOne;
     }

 }

相關(guān)文章

最新評(píng)論

怀宁县| 长武县| 荔波县| 老河口市| 尉氏县| 叶城县| 岫岩| 游戏| 海兴县| 五台县| 大埔区| 安徽省| 博客| 康乐县| 锡林浩特市| 花垣县| 凤城市| 鄯善县| 五常市| 称多县| 崇州市| 永春县| 南木林县| 沙湾县| 株洲市| 垦利县| 阿巴嘎旗| 谷城县| 台湾省| 东港市| 邵东县| 池州市| 保山市| 松阳县| 湘潭县| 高青县| 淄博市| 临澧县| 灵川县| 宝鸡市| 宕昌县|