2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > java实现日历(某年的日历 某月的日历)用户完全自定义

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

时间:2020-11-26 18:27:47

相关推荐

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

每个人都希望每天都是开心的,不要因为一些琐事扰乱了心情还,闲暇的时间怎么打发,关注可以让你学习更多的好东西,下面为大家推荐java实现日历(某年的日历,某月的日历)用户完全自定义,赶紧看过来吧!

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

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

代码如下:

January

---------------------------------------------

Sun Mon Tue Wed Thu Fri Sat

2345

78910 11 12

14 15 16 17 18 19

21 22 23 24 25 26

28 29 30 31

Febuary

---------------------------------------------

Sun Mon Tue Wed Thu Fri Sat

2

456789

11 12 13 14 15 16

18 19 20 21 22 23

25 26 27 28

March

---------------------------------------------

Sun Mon Tue Wed Thu Fri Sat

2

456789

11 12 13 14 15 16

18 19 20 21 22 23

25 26 27 28 29 30

April

---------------------------------------------

Sun Mon Tue Wed Thu Fri Sat

23456

8910 11 12 13

15 16 17 18 19 20

22 23 24 25 26 27

29 30

May

---------------------------------------------

Sun Mon Tue Wed Thu Fri Sat

234

678910 11

13 14 15 16 17 18

20 21 22 23 24 25

27 28 29 30 31

Jun

---------------------------------------------

Sun Mon Tue Wed Thu Fri Sat

345678

10 11 12 13 14 15

17 18 19 20 21 22

24 25 26 27 28 29

July

---------------------------------------------

Sun Mon Tue Wed Thu Fri Sat

23456

8910 11 12 13

15 16 17 18 19 20

22 23 24 25 26 27

29 30 31

August

---------------------------------------------

Sun Mon Tue Wed Thu Fri Sat

23

5678910

12 13 14 15 16 17

19 20 21 22 23 24

26 27 28 29 30 31

Septermber

---------------------------------------------

Sun Mon Tue Wed Thu Fri Sat

234567

910 11 12 13 14

16 17 18 19 20 21

23 24 25 26 27 28

30

October

---------------------------------------------

Sun Mon Tue Wed Thu Fri Sat

2345

78910 11 12

14 15 16 17 18 19

21 22 23 24 25 26

28 29 30 31

November

---------------------------------------------

Sun Mon Tue Wed Thu Fri Sat

2

456789

11 12 13 14 15 16

18 19 20 21 22 23

25 26 27 28 29 30

December

---------------------------------------------

Sun Mon Tue Wed Thu Fri Sat

234567

910 11 12 13 14

16 17 18 19 20 21

23 24 25 26 27 28

30 31

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

如打印:1月份的日历

代码如下:

日 一 二 三 四 五 六

234

6789 10 11

13 14 15 16 17 18

20 21 22 23 24 25

27 28 29 30 31

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

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

代码如下:

日 一 二 三 四 五 六

23456

89 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/

代码如下:

/**

*

*/

package com.;

import ;

import ;

import ;

import ;

/**

* 一个日期处理类,在该类中,构造函数codeHongtenDate()/code,系统会默认设置年份为当年br

* 而codeHongtenDate(int year)/code,则可以自定义年份br

*

* pre

* HongtenDate date = new HongtenDate();

* /pre

*

* orbr

*

* pre

* HongtenDate hd = new HongtenDate();

* /pre

*

* 调用codeprintCalendar()/code可以打印一年的日期br

* 调用codeprintCurrentMonth()/code可以打印当前月的日期br

* ,当然,你也可以调用br

* codeprintMonthOfYear()/code设置一个参数,进行打印某个月的日期br

* 这里提供一些参考方案:br

*

* pre

* // 无参数,系统默认去当前年份

* HongtenDate date = new HongtenDate();

* ();

* ();

* ();

* /pre

*

* orbr

*

* pre

* // 设置为

* HongtenDate hd = new HongtenDate();

* ();

* ();

* /pre

*

* @date -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 StringOCTOBER = "October";

public static final String NOVEMBER = "November";

public static final String DECEMBER = "December";

/**

* 年份

*/

private int year;

/**

* 一月一日星期几(星期二,所以 = 2;/code)

*/

private int whatDayOnJanuaryOne;

// main

// ======================================

public static void main(String[] args) throws Exception {

// 无参数,系统默认去当前年份

HongtenDate date = new HongtenDate();

// ();

();

// ();

// 设置为

HongtenDate hd = new HongtenDate();

// ();

// ();

}

// 无参数,系统默认去当前年份

public HongtenDate() {

Calendar cal = ();

= ();

try {

setWhatDayOnJanuaryOne(getJanuaryOne(year));

} catch (Exception e) {

();

}

}

// 有参数,设置年份

public HongtenDate(int year) {

= year;

try {

setWhatDayOnJanuaryOne(getJanuaryOne(year));

} catch (Exception e) {

();

}

}

/**

* 打印某个月的所有日期

*

* @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 = ();

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 ( % 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 + " " + + "");

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) {

= year;

}

public int getWhatDayOnJanuaryOne() {

return whatDayOnJanuaryOne;

}

public void setWhatDayOnJanuaryOne(int whatDayOnJanuaryOne) {

this.whatDayOnJanuaryOne = whatDayOnJanuaryOne;

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。