zoukankan      html  css  js  c++  java
  • 日历(Calendar)_java版(某年的日历,某月的日历)_用户完全自定义

    这个想法源自于博客园的日历......

    用户可以自定义打印某一年的年历,即:把某一年的日历全部打印出来

    如把2013年的年历打印出来如下:

                 January  2013          
    ---------------------------------------------
       Sun   Mon   Tue   Wed   Thu   Fri   Sat
                   1     2     3     4     5
       6     7     8     9     10    11    12
       13    14    15    16    17    18    19
       20    21    22    23    24    25    26
       27    28    29    30    31
    
                 Febuary  2013          
    ---------------------------------------------
       Sun   Mon   Tue   Wed   Thu   Fri   Sat
                                     1     2
       3     4     5     6     7     8     9
       10    11    12    13    14    15    16
       17    18    19    20    21    22    23
       24    25    26    27    28
    
                 March  2013          
    ---------------------------------------------
       Sun   Mon   Tue   Wed   Thu   Fri   Sat
                                     1     2
       3     4     5     6     7     8     9
       10    11    12    13    14    15    16
       17    18    19    20    21    22    23
       24    25    26    27    28    29    30
       31
    
                 April  2013          
    ---------------------------------------------
       Sun   Mon   Tue   Wed   Thu   Fri   Sat
             1     2     3     4     5     6
       7     8     9     10    11    12    13
       14    15    16    17    18    19    20
       21    22    23    24    25    26    27
       28    29    30
    
                 May  2013          
    ---------------------------------------------
       Sun   Mon   Tue   Wed   Thu   Fri   Sat
                         1     2     3     4
       5     6     7     8     9     10    11
       12    13    14    15    16    17    18
       19    20    21    22    23    24    25
       26    27    28    29    30    31
    
                 Jun  2013          
    ---------------------------------------------
       Sun   Mon   Tue   Wed   Thu   Fri   Sat
                                           1  
       2     3     4     5     6     7     8
       9     10    11    12    13    14    15
       16    17    18    19    20    21    22
       23    24    25    26    27    28    29
       30
    
                 July  2013          
    ---------------------------------------------
       Sun   Mon   Tue   Wed   Thu   Fri   Sat
             1     2     3     4     5     6
       7     8     9     10    11    12    13
       14    15    16    17    18    19    20
       21    22    23    24    25    26    27
       28    29    30    31
    
                 August  2013          
    ---------------------------------------------
       Sun   Mon   Tue   Wed   Thu   Fri   Sat
                               1     2     3
       4     5     6     7     8     9     10
       11    12    13    14    15    16    17
       18    19    20    21    22    23    24
       25    26    27    28    29    30    31
    
                 Septermber  2013          
    ---------------------------------------------
       Sun   Mon   Tue   Wed   Thu   Fri   Sat
       1     2     3     4     5     6     7
       8     9     10    11    12    13    14
       15    16    17    18    19    20    21
       22    23    24    25    26    27    28
       29    30
    
                 October  2013          
    ---------------------------------------------
       Sun   Mon   Tue   Wed   Thu   Fri   Sat
                   1     2     3     4     5
       6     7     8     9     10    11    12
       13    14    15    16    17    18    19
       20    21    22    23    24    25    26
       27    28    29    30    31
    
                 November  2013          
    ---------------------------------------------
       Sun   Mon   Tue   Wed   Thu   Fri   Sat
                                     1     2
       3     4     5     6     7     8     9
       10    11    12    13    14    15    16
       17    18    19    20    21    22    23
       24    25    26    27    28    29    30
    
                 December  2013          
    ---------------------------------------------
       Sun   Mon   Tue   Wed   Thu   Fri   Sat
       1     2     3     4     5     6     7
       8     9     10    11    12    13    14
       15    16    17    18    19    20    21
       22    23    24    25    26    27    28
       29    30    31

    当然用户如果想单独打印某个月的日历,同样是可以实现的

    如打印:2014年1月份的日历

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

    用户还可以实现打印当前的月份的日历

    今天是:2013-04-27,则当前月份的日历打印如下:

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

    是的,你没有看错,在27的那里有一个标志,表示是当天的日期.....

    下面进入代码部分:

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

    代码部分:

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

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

      1 /**
      2  * 
      3  */
      4 package com.b510.date;
      5 
      6 import java.text.SimpleDateFormat;
      7 import java.util.Calendar;
      8 import java.util.Date;
      9 import java.util.GregorianCalendar;
     10 
     11 /**
     12  * 一个日期处理类,在该类中,构造函数<code>HongtenDate()</code>,系统会默认设置年份为当年<br>
     13  * 而<code>HongtenDate(int year)</code>,则可以自定义年份<br>
     14  * 
     15  * <pre>
     16  * HongtenDate date = new HongtenDate();
     17  * </pre>
     18  * 
     19  * or<br>
     20  * 
     21  * <pre>
     22  * HongtenDate hd = new HongtenDate(2014);
     23  * </pre>
     24  * 
     25  * 调用<code>printCalendar()</code>可以打印一年的日期<br>
     26  * 调用<code>printCurrentMonth()</code>可以打印当前月的日期<br>
     27  * ,当然,你也可以调用<br>
     28  * <code>printMonthOfYear()</code>设置一个参数,进行打印某个月的日期<br>
     29  * 这里提供一些参考方案:<br>
     30  * 
     31  * <pre>
     32  * // 无参数,系统默认去当前年份
     33  * HongtenDate date = new HongtenDate();
     34  * date.printCalendar();
     35  * date.printCurrentMonth();
     36  * date.printMonthOfYear(4);
     37  * </pre>
     38  * 
     39  * or<br>
     40  * 
     41  * <pre>
     42  * // 设置为2014年
     43  * HongtenDate hd = new HongtenDate(2014);
     44  * hd.printCurrentMonth();
     45  * hd.printMonthOfYear(1);
     46  * </pre>
     47  * 
     48  * @date 2013-4-27
     49  * @author hongten
     50  * 
     51  */
     52 public class HongtenDate {
     53 
     54     // MONTHS
     55     // ============================================
     56     public static final String JANUARY = "January";
     57     public static final String FEBUARY = "Febuary";
     58     public static final String MARCH = "March";
     59     public static final String APRIL = "April";
     60     public static final String MAY = "May";
     61     public static final String JUN = "Jun";
     62     public static final String JULY = "July";
     63     public static final String AUGUST = "August";
     64     public static final String SEPTERMBER = "Septermber";
     65     public static final String OCTOBER = "October";
     66     public static final String NOVEMBER = "November";
     67     public static final String DECEMBER = "December";
     68 
     69     /**
     70      * 年份
     71      */
     72     private int year;
     73     /**
     74      * 一月一日星期几(eg:2013-01-01-->星期二,所以<code>whatDayOnJanuaryOne = 2;</code>)
     75      */
     76     private int whatDayOnJanuaryOne;
     77 
     78     // main
     79     // ======================================
     80     public static void main(String[] args) throws Exception {
     81         // 无参数,系统默认去当前年份
     82         HongtenDate date = new HongtenDate();
     83         // date.printCalendar();
     84         date.printCurrentMonth();
     85         // date.printMonthOfYear(4);
     86 
     87         // 设置为2014年
     88         HongtenDate hd = new HongtenDate(2014);
     89         // hd.printCurrentMonth();
     90         // hd.printMonthOfYear(1);
     91 
     92     }
     93 
     94     // 无参数,系统默认去当前年份
     95     public HongtenDate() {
     96         Calendar cal = Calendar.getInstance();
     97         this.year = cal.get(Calendar.YEAR);
     98         try {
     99             setWhatDayOnJanuaryOne(getJanuaryOne(year));
    100         } catch (Exception e) {
    101             e.printStackTrace();
    102         }
    103     }
    104 
    105     // 有参数,设置年份
    106     public HongtenDate(int year) {
    107         this.year = year;
    108         try {
    109             setWhatDayOnJanuaryOne(getJanuaryOne(year));
    110         } catch (Exception e) {
    111             e.printStackTrace();
    112         }
    113     }
    114 
    115     /**
    116      * 打印某个月的所有日期
    117      * 
    118      * @param mon
    119      *            月份
    120      * @throws Exception
    121      */
    122     public void printMonthOfYear(int mon) throws Exception {
    123         if (mon < 1 || mon > 12) {
    124             System.out.println("你输入的月份[" + mon + "]不对,请检查在进行....");
    125             return;
    126         }
    127         GregorianCalendar now = new GregorianCalendar();
    128         // 获得一个Date对象
    129         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    130         Date date = sdf.parse(year + "-" + mon + "-01");
    131         // 设置当前时间
    132         now.setTime(date);
    133         // 从日期中取得当前的月
    134         int month = now.get(Calendar.MONTH);
    135         // 设置now的日期为1
    136         now.set(Calendar.DAY_OF_MONTH, 1);
    137         // 得到now是一周的第几天
    138         int week = now.get(Calendar.DAY_OF_WEEK);
    139         // 打印日历头部标示
    140         System.out.println("日\t一\t二\t三\t四\t五\t六");
    141         // 打印当前日期前面的空格
    142         for (int i = Calendar.SUNDAY; i < week; i++) {
    143             System.out.print("\t");
    144         }
    145         // 打印日历主体
    146         while (now.get(Calendar.MONTH) == month) {
    147             int day = now.get(Calendar.DAY_OF_MONTH);
    148             // 对输出的日历进行对齐,小于10的加上一个空格
    149             if (day < 10) {
    150                 System.out.print(" " + day + "\t");
    151             } else {
    152                 System.out.print("" + day + "\t");
    153             }
    154             // 如果是周六,进行换行
    155             if (week == Calendar.SATURDAY) {
    156                 System.out.println();
    157             }
    158             // 每次输出日期后,将日期增加一天
    159             now.add(Calendar.DAY_OF_MONTH, 1);
    160             // 重新获得一周的第几天
    161             week = now.get(Calendar.DAY_OF_WEEK);
    162         }
    163     }
    164 
    165     /**
    166      * 打印当前月的所有日期,这个不会因用户设置的年份而改变
    167      */
    168     public void printCurrentMonth() {
    169         GregorianCalendar now = new GregorianCalendar();
    170         // 获得一个Date对象
    171         Date date = new Date();
    172         // 设置当前时间
    173         now.setTime(date);
    174         // 从日期中取得当前的日
    175         int toDay = now.get(Calendar.DAY_OF_MONTH);
    176         // 从日期中取得当前的月
    177         int month = now.get(Calendar.MONTH);
    178         // 设置now的日期为1
    179         now.set(Calendar.DAY_OF_MONTH, 1);
    180         // 得到now是一周的第几天
    181         int week = now.get(Calendar.DAY_OF_WEEK);
    182         // 打印日历头部标示
    183         System.out.println("日\t一\t二\t三\t四\t五\t六");
    184         // 打印当前日期前面的空格
    185         for (int i = Calendar.SUNDAY; i < week; i++) {
    186             System.out.print("\t");
    187         }
    188         // 打印日历主体
    189         while (now.get(Calendar.MONTH) == month) {
    190             int day = now.get(Calendar.DAY_OF_MONTH);
    191             // 对输出的日历进行对齐,小于10的加上一个空格
    192             if (day < 10) {
    193                 // 如果是当前日期,加上标示
    194                 if (day == toDay) {
    195                     System.out.print(":)" + day + "(:\t");
    196                 } else {
    197                     System.out.print(" " + day + "\t");
    198                 }
    199             } else {
    200                 // 如果是当前日期,加上标示
    201                 if (day == toDay) {
    202                     System.out.print(":)" + day + "(:\t");
    203                 } else {
    204                     System.out.print("" + day + "\t");
    205                 }
    206             }
    207             // 如果是周六,进行换行
    208             if (week == Calendar.SATURDAY) {
    209                 System.out.println();
    210             }
    211             // 每次输出日期后,将日期增加一天
    212             now.add(Calendar.DAY_OF_MONTH, 1);
    213             // 重新获得一周的第几天
    214             week = now.get(Calendar.DAY_OF_WEEK);
    215         }
    216     }
    217 
    218     /**
    219      * 获取year这一年的一月一号是星期几
    220      * 
    221      * @param year
    222      *            年份
    223      * @return
    224      * @throws Exception
    225      */
    226     public int getJanuaryOne(int year) throws Exception {
    227         int[] weekDays = { 0, 1, 2, 3, 4, 5, 6 };
    228         Calendar cal = Calendar.getInstance();
    229         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    230         Date dt = sdf.parse(year + "-01-01");
    231         cal.setTime(dt);
    232         int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
    233         if (w < 0)
    234             w = 0;
    235         return weekDays[w];
    236     }
    237 
    238     /**
    239      * 打印一年的所有月份
    240      */
    241     public void printCalendar() {
    242         for (int i = 1; i <= 12; i++) {
    243             String month = getMonth(i);
    244             printTitle(month);
    245             // 打印有31天的月份
    246             if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) {
    247                 print31();
    248             }
    249             // 打印有30天的月份
    250             else if (i == 4 || i == 6 || i == 9 || i == 11) {
    251                 print30();
    252             }
    253             // 打印二月份
    254             else if (i == 2) {
    255                 printFebuary();
    256             }
    257             System.out.println();
    258         }
    259     }
    260 
    261     // 打印格式
    262     // ============================================== start
    263     /**
    264      * 打印二月份,每一年的二月份可能不相同,所以要单独处理
    265      */
    266     protected void printFebuary() {
    267         if (this.year % 4 == 0) {
    268             // 闰年
    269             printLeapYear();
    270         } else {
    271             // 平年
    272             printNonleapYear();
    273         }
    274     }
    275 
    276     /**
    277      * 闰年二月份打印格式
    278      */
    279     private void printLeapYear() {
    280         for (int j = 1; j <= 29; j++) {
    281             String tmp = "";
    282             if (j == 1) {
    283                 for (int k = 1; k <= this.whatDayOnJanuaryOne % 7; k++) {
    284                     tmp = tmp + "      ";
    285                 }
    286                 tmp = tmp + "   " + j + "  ";
    287                 if (this.whatDayOnJanuaryOne == 6) {
    288                     System.out.println(tmp);
    289                 } else {
    290                     System.out.print(tmp);
    291                 }
    292             } else if (j > 1 && j < 29) {
    293 
    294                 if ((this.whatDayOnJanuaryOne + j) % 7 == 0) {
    295                     System.out.println("   " + j);
    296                 } else {
    297                     if (j < 10) {
    298                         System.out.print("   " + j + "  ");
    299                     } else {
    300                         System.out.print("   " + j + " ");
    301                     }
    302                 }
    303             } else if (j == 29) {
    304                 System.out.println("   " + j);
    305                 this.whatDayOnJanuaryOne = ((this.whatDayOnJanuaryOne + j) % 7);
    306             }
    307         }
    308     }
    309 
    310     /**
    311      * 打印平年二月份格式
    312      */
    313     private void printNonleapYear() {
    314         for (int j = 1; j <= 28; j++) {
    315             String tmp = "";
    316             if (j == 1) {
    317                 for (int k = 1; k <= this.whatDayOnJanuaryOne % 7; k++) {
    318                     tmp = tmp + "      ";
    319                 }
    320                 tmp = tmp + "   " + j + "  ";
    321                 if (this.whatDayOnJanuaryOne == 6) {
    322                     System.out.println(tmp);
    323                 } else {
    324                     System.out.print(tmp);
    325                 }
    326             } else if (j > 1 && j < 28) {
    327 
    328                 if ((this.whatDayOnJanuaryOne + j) % 7 == 0) {
    329                     System.out.println("   " + j);
    330                 } else {
    331                     if (j < 10) {
    332                         System.out.print("   " + j + "  ");
    333                     } else {
    334                         System.out.print("   " + j + " ");
    335                     }
    336                 }
    337             } else if (j == 28) {
    338                 System.out.println("   " + j);
    339                 this.whatDayOnJanuaryOne = ((this.whatDayOnJanuaryOne + j) % 7);
    340             }
    341         }
    342     }
    343 
    344     /**
    345      * 打印有30天的月份
    346      */
    347     protected void print30() {
    348         for (int j = 1; j <= 30; j++) {
    349             String tmp = "";
    350             if (j == 1) {
    351                 for (int k = 1; k <= this.whatDayOnJanuaryOne % 7; k++) {
    352                     tmp = tmp + "      ";
    353                 }
    354                 tmp = tmp + "   " + j + "  ";
    355                 if (this.whatDayOnJanuaryOne == 6) {
    356                     System.out.println(tmp);
    357                 } else {
    358                     System.out.print(tmp);
    359                 }
    360             } else if (j > 1 && j < 30) {
    361 
    362                 if ((this.whatDayOnJanuaryOne + j) % 7 == 0) {
    363                     System.out.println("   " + j);
    364                 } else {
    365                     if (j < 10) {
    366                         System.out.print("   " + j + "  ");
    367                     } else {
    368                         System.out.print("   " + j + " ");
    369                     }
    370                 }
    371             } else if (j == 30) {
    372                 System.out.println("   " + j);
    373                 this.whatDayOnJanuaryOne = ((this.whatDayOnJanuaryOne + j) % 7);
    374             }
    375         }
    376     }
    377 
    378     /**
    379      * 打印有31天的月份
    380      */
    381     protected void print31() {
    382         for (int j = 1; j <= 31; j++) {
    383             String tmp = "";
    384             if (j == 1) {
    385                 for (int k = 1; k <= this.whatDayOnJanuaryOne % 7; k++) {
    386                     tmp = tmp + "      ";
    387                 }
    388                 tmp = tmp + "   " + j + "  ";
    389                 if (this.whatDayOnJanuaryOne == 6) {
    390                     System.out.println(tmp);
    391                 } else {
    392                     System.out.print(tmp);
    393                 }
    394             } else if (j > 1 && j < 31) {
    395 
    396                 if ((this.whatDayOnJanuaryOne + j) % 7 == 0) {
    397                     System.out.println("   " + j);
    398                 } else {
    399                     if (j < 10) {
    400                         System.out.print("   " + j + "  ");
    401                     } else {
    402                         System.out.print("   " + j + " ");
    403                     }
    404                 }
    405             } else if (j == 31) {
    406                 System.out.println("   " + j);
    407                 this.whatDayOnJanuaryOne = ((this.whatDayOnJanuaryOne + j) % 7);
    408             }
    409         }
    410     }
    411 
    412     /**
    413      * 打印每个月的标题
    414      * 
    415      * @param month
    416      */
    417     protected void printTitle(String month) {
    418         System.out.println("             " + month + "  " + this.year + "          ");
    419         System.out.println("---------------------------------------------");
    420         System.out.println("   Sun   Mon   Tue   Wed   Thu   Fri   Sat");
    421     }
    422 
    423     // 打印格式
    424     // ============================================== end
    425 
    426     /**
    427      * 获取月份的英文名称
    428      * 
    429      * @param m
    430      *            月份的数字表示
    431      * @return
    432      */
    433     public String getMonth(int m) {
    434         String month = "";
    435         switch (m) {
    436         case 1:
    437             month = JANUARY;
    438             break;
    439         case 2:
    440             month = FEBUARY;
    441             break;
    442         case 3:
    443             month = MARCH;
    444             break;
    445         case 4:
    446             month = APRIL;
    447             break;
    448         case 5:
    449             month = MAY;
    450             break;
    451         case 6:
    452             month = JUN;
    453             break;
    454         case 7:
    455             month = JULY;
    456             break;
    457         case 8:
    458             month = AUGUST;
    459             break;
    460         case 9:
    461             month = SEPTERMBER;
    462             break;
    463         case 10:
    464             month = OCTOBER;
    465             break;
    466         case 11:
    467             month = NOVEMBER;
    468             break;
    469         case 12:
    470             month = DECEMBER;
    471             break;
    472         }
    473         return month;
    474     }
    475 
    476     public int getYear() {
    477         return year;
    478     }
    479 
    480     public void setYear(int year) {
    481         this.year = year;
    482     }
    483 
    484     public int getWhatDayOnJanuaryOne() {
    485         return whatDayOnJanuaryOne;
    486     }
    487 
    488     public void setWhatDayOnJanuaryOne(int whatDayOnJanuaryOne) {
    489         this.whatDayOnJanuaryOne = whatDayOnJanuaryOne;
    490     }
    491 
    492 }
  • 相关阅读:
    A1061 Dating [字符串比较]
    A1058 A+B in Hogwarts [进制转换]
    A1027 Colors in Mars[简单模拟--进制转换]
    A1031 Hello World for U[图形输出]
    刷PAT的一些思考—Day 1
    A1009 Product of Polynomials[简单模拟--多项式相乘]
    A1002 A+B for Polynomials[简单模拟--多项式相加]
    A1046 Shortest Distance [简单模拟--卡算法复杂度]
    1009 说反话
    usb之配置描述符
  • 原文地址:https://www.cnblogs.com/hongten/p/hongten_calendar_date.html
Copyright © 2011-2022 走看看