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

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

如把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;
     }

 }

时间: 2024-08-07 20:44:31

java实现日历(某年的日历,某月的日历)用户完全自定义_java的相关文章

求代码-java程序的编写一个指定的年月的日历

问题描述 java程序的编写一个指定的年月的日历 就是我是新手,要用java编一个输出2014年8月的日历怎么写啊,求代码 解决方案 是java代码呢还是web工程呢,如果是java代码的话,使用日历类Calendar类,输出某个月之间所有的日期就可以了. web 工程的话,有现成的日历控件My97DatePicker可以使用的.

Java 从键盘中输入年份与月份,输出该月的日历

问题描述 Java 从键盘中输入年份与月份,输出该月的日历 如:2015年4月 星期日 星期一 星期二 星期三 星期四 星期五 星期六 1 2 3 4 ............. 解决方案 http://bbs.csdn.net/topics/370043525 解决方案二: 主要是日期和星期的对应关系不好找吗?可以把电脑的日期设置成输入的月份的第一天,再取这一天的时间,时间结构里就有星期,再一天一天往后数. 说实话我是在小说里看到的这个方法... 解决方案三: <input type="

javascript carlengar 脚本日历的,不要正常显示,,要点击日历的左右按钮才能显示日期,求高手指教 [.NET

问题描述 javascript脚本日历的,不要正常显示,,要点击日历的左右按钮才能显示日期,求高手指教如何让它正常显示:日历代码:--------------------------<ScriptLANGUAGE="JavaScript">varmonths=newArray("一","二","三","四","五","六","七",&q

切换用户实现自定义日历转换,点击日期调用监听

问题描述 切换用户实现自定义日历转换,点击日期调用监听 安卓怎样实现点击切换用户之后日历信息更改,之后点击某一天具体日期,出现对应用户的内容?自定义日历,日历点击事件 我现在上面的日历数据更改已经实现好了,但是下面的具体课程内容并没有实现好,求大神指点.已经困扰妹子好几天了.我知道是没有调用到点击日期的监听,但是我写了,压根不会进监听里面.在线等待大神指导... 解决方案 在线等大神解救我..不知道这个问题会不会沉下去了

人生日历让你DIY自己的桌面日历

&http://www.aliyun.com/zixun/aggregation/37954.html">nbsp;   办公室的童鞋们,每天对着电脑,缺乏乐趣?生活中的乐趣常常要自己寻找,每一点细节的变化,都能带来不一样的感觉,是时候给自己的电脑换个有趣的桌面日历了!DIY,没错桌面日历实现DIY了,完全可以由你做主!       你的桌面你做主,想怎么玩?我来告诉你.拿起鼠标点击日历界面右下角的应用按钮,马上弹出插件应用管理框,看到了吧?里面的应用可自定义排序,还可以帮桌面的应

jsp 网站开发-JAVA WEB应用,一个操作只能同时有一个用户操作

问题描述 JAVA WEB应用,一个操作只能同时有一个用户操作 一个B/S系统,其中有个调用Linux脚本的操作,执行时间较长,要求系统只能同时有一个用户执行此操作,否则提示其他用户正在执行,请问下该如何实现呢? 解决方案 在該腳本開頭添加"ps -ef | grep 'yourshell'",返回監測結果,看是繼續執行,或是退出交回到java 解决方案二: 这个可以当启动linux脚本时写一个标识位到一个文件内.当其它用户再启动linux脚本时检测这个文件.是否存在即可. 解决方案三

久邦数码旗下GO Launcher EX第三季度月平均活跃用户达4200万

3G门户母公司久邦数码今日向美国证券交易委员会(SEC)提交了IPO(首次公开招股)招股书.久邦数码旗下GO Launcher EX在2013年第三季度月平均活跃用户达4200万,3G门户(3G.cn)在2013年9月的独立用户量达到4.4亿. 招股书显示,在截至2013年9月30日的9个月内,久邦数码是Google Play应用类中全球下载量最大的三大发行商之一. 久邦数码的平台产品GO Launcher EX可以管理Android智能手机的应用.widget和功能.根据App Annie I

java程序员用ssh+oracle怎么把一张用户数据大表拆分成几个表

问题描述 java程序员用ssh+oracle怎么把一张用户数据大表拆分成几个表,有没有现成的博客可以查看学习,例子demo 解决方案 解决方案二:可以利用view啊,把大表生成不同的view解决方案三:oracle分区解决方案四:这个不是用程序来做的,直接操作数据库.分区处理

入行2个月,总是对android自定义view望而却步,有什么书籍吗

问题描述 入行2个月,总是对android自定义view望而却步,有什么书籍吗 想找些资料系统的研究一下自定义View,有书籍推荐吗,不想做代码搬运工啊,想跨过这道坎. 解决方案 <Android群英传>和<Android开发艺术探究> 解决方案二: <Android开发权威指南(第二版)>. <android开发范例大全>, 解决方案三: 疯狂Android讲义 解决方案四: CSDN上有很多大神写过自定义View的博客,写的非常详细,效果也很赞,建议你去