欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现日历(某年的日历,某月的日历)用户完全自定义

程序员文章站 2023-12-05 21:09:40
用户可以自定义打印某一年的年历,即:把某一年的日历全部打印出来 如把2013年的年历打印出来如下:复制代码 代码如下:january  2013 &n...

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

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

复制代码 代码如下:

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


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

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

复制代码 代码如下:

日    一    二    三    四    五    六
     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

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

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

复制代码 代码如下:

日    一    二    三    四    五    六
     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

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

下面进入代码部分:

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

代码部分:

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

/uuuuuu_test/src/com/b510/date/hongtendate.java

复制代码 代码如下:

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

 import java.text.simpledateformat;
 import java.util.calendar;
 import java.util.date;
 import java.util.gregoriancalendar;

 /**
  * 一个日期处理类,在该类中,构造函数<code>hongtendate()</code>,系统会默认设置年份为当年<br>
  * 而<code>hongtendate(int year)</code>,则可以自定义年份<br>
  *
  * <pre>
  * hongtendate date = new hongtendate();
  * </pre>
  *
  * or<br>
  *
  * <pre>
  * hongtendate hd = new hongtendate(2014);
  * </pre>
  *
  * 调用<code>printcalendar()</code>可以打印一年的日期<br>
  * 调用<code>printcurrentmonth()</code>可以打印当前月的日期<br>
  * ,当然,你也可以调用<br>
  * <code>printmonthofyear()</code>设置一个参数,进行打印某个月的日期<br>
  * 这里提供一些参考方案:<br>
  *
  * <pre>
  * // 无参数,系统默认去当前年份
  * hongtendate date = new hongtendate();
  * date.printcalendar();
  * date.printcurrentmonth();
  * date.printmonthofyear(4);
  * </pre>
  *
  * or<br>
  *
  * <pre>
  * // 设置为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 {
         // 无参数,系统默认去当前年份
         hongtendate date = new hongtendate();
         // date.printcalendar();
         date.printcurrentmonth();
         // date.printmonthofyear(4);

         // 设置为2014年
         hongtendate hd = new hongtendate(2014);
         // hd.printcurrentmonth();
         // hd.printmonthofyear(1);

     }

     // 无参数,系统默认去当前年份
     public hongtendate() {
         calendar cal = calendar.getinstance();
         this.year = cal.get(calendar.year);
         try {
             setwhatdayonjanuaryone(getjanuaryone(year));
         } catch (exception e) {
             e.printstacktrace();
         }
     }

     // 有参数,设置年份
     public hongtendate(int year) {
         this.year = year;
         try {
             setwhatdayonjanuaryone(getjanuaryone(year));
         } catch (exception e) {
             e.printstacktrace();
         }
     }

     /**
      * 打印某个月的所有日期
      *
      * @param mon
      *            月份
      * @throws exception
      */
     public void printmonthofyear(int mon) throws exception {
         if (mon < 1 || mon > 12) {
             system.out.println("你输入的月份[" + mon + "]不对,请检查在进行....");
             return;
         }
         gregoriancalendar now = new gregoriancalendar();
         // 获得一个date对象
         simpledateformat sdf = new simpledateformat("yyyy-mm-dd");
         date date = sdf.parse(year + "-" + mon + "-01");
         // 设置当前时间
         now.settime(date);
         // 从日期中取得当前的月
         int month = now.get(calendar.month);
         // 设置now的日期为1
         now.set(calendar.day_of_month, 1);
         // 得到now是一周的第几天
         int week = now.get(calendar.day_of_week);
         // 打印日历头部标示
         system.out.println("日\t一\t二\t三\t四\t五\t六");
         // 打印当前日期前面的空格
         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);
             // 对输出的日历进行对齐,小于10的加上一个空格
             if (day < 10) {
                 system.out.print(" " + day + "\t");
             } else {
                 system.out.print("" + day + "\t");
             }
             // 如果是周六,进行换行
             if (week == calendar.saturday) {
                 system.out.println();
             }
             // 每次输出日期后,将日期增加一天
             now.add(calendar.day_of_month, 1);
             // 重新获得一周的第几天
             week = now.get(calendar.day_of_week);
         }
     }

     /**
      * 打印当前月的所有日期,这个不会因用户设置的年份而改变
      */
     public void printcurrentmonth() {
         gregoriancalendar now = new gregoriancalendar();
         // 获得一个date对象
         date date = new date();
         // 设置当前时间
         now.settime(date);
         // 从日期中取得当前的日
         int today = now.get(calendar.day_of_month);
         // 从日期中取得当前的月
         int month = now.get(calendar.month);
         // 设置now的日期为1
         now.set(calendar.day_of_month, 1);
         // 得到now是一周的第几天
         int week = now.get(calendar.day_of_week);
         // 打印日历头部标示
         system.out.println("日\t一\t二\t三\t四\t五\t六");
         // 打印当前日期前面的空格
         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);
             // 对输出的日历进行对齐,小于10的加上一个空格
             if (day < 10) {
                 // 如果是当前日期,加上标示
                 if (day == today) {
                     system.out.print(":)" + day + "(:\t");
                 } else {
                     system.out.print(" " + day + "\t");
                 }
             } else {
                 // 如果是当前日期,加上标示
                 if (day == today) {
                     system.out.print(":)" + day + "(:\t");
                 } else {
                     system.out.print("" + day + "\t");
                 }
             }
             // 如果是周六,进行换行
             if (week == calendar.saturday) {
                 system.out.println();
             }
             // 每次输出日期后,将日期增加一天
             now.add(calendar.day_of_month, 1);
             // 重新获得一周的第几天
             week = now.get(calendar.day_of_week);
         }
     }

     /**
      * 获取year这一年的一月一号是星期几
      *
      * @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
     /**
      * 打印二月份,每一年的二月份可能不相同,所以要单独处理
      */
     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);
             }
         }
     }

     /**
      * 打印每个月的标题
      *
      * @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

     /**
      * 获取月份的英文名称
      *
      * @param m
      *            月份的数字表示
      * @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;
     }

 }