Java实现的日历功能完整示例

时间:2022-11-04 09:11:37

本文实例讲述了java实现的日历功能。分享给大家供大家参考,具体如下:

应用名称:java日历

用到的知识:java gui编程,日期操作

开发环境:win8+eclipse+jdk1.8

功能说明:一个很简单的万年历,可以选择年份和月份,也可以用按钮翻页,日历会实时更新日期,最下方会显示当前操作系统的时间。

效果图:

Java实现的日历功能完整示例

源代码:

calendarframe.java

?
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import java.awt.borderlayout;
import java.awt.gridlayout;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.text.simpledateformat;
import java.util.date;
import javax.swing.jbutton;
import javax.swing.jcombobox;
import javax.swing.jframe;
import javax.swing.jlabel;
import javax.swing.jpanel;
import javax.swing.border.bevelborder;
import javax.swing.border.softbevelborder;
public class calendarframe extends jframe implements actionlistener{
  /**
   * @author nut
   * 2016.01.13
   */
  private static final long serialversionuid = -7260798316896145633l;
  jlabel labelday[] = new jlabel[42];
  jbutton titlename[] = new jbutton[7];
  string name[]={"日","一","二","三","四","五","六"};
  jbutton nextmonth,previousmonth;
  jcombobox choiceyear,choicemonth;
  calendarbean calendar;
  jlabel showyear,showmonth;
  jlabel showmessage=new jlabel("",jlabel.center);
  int year = 2011,month=2;
  //构造方法初始化界面
  public calendarframe(){
    jpanel pcenter = new jpanel();
    pcenter.setlayout(new gridlayout(7,7));
    //星期栏
    for(int i=0;i<7;i++){
      titlename[i]=new jbutton(name[i]);
      titlename[i].setborder(new softbevelborder(bevelborder.raised));
      pcenter.add(titlename[i]);
    }
    //日期栏
    for(int i=0;i<42;i++){
      labelday[i]=new jlabel("",jlabel.center);
      labelday[i].setborder(new softbevelborder(bevelborder.lowered));
      pcenter.add(labelday[i]);
    }
    //年月选择栏
    choiceyear=new jcombobox();
    choicemonth=new jcombobox();
    showyear=new jlabel("年");
    showmonth=new jlabel("月  ");
    for(int i=1990;i<2050;i++)
      choiceyear.additem(i);
    choiceyear.addactionlistener(this);
    for(int i=1;i<=12;i++)
      choicemonth.additem(i);
    choicemonth.addactionlistener(this);
    calendar=new calendarbean();
    nextmonth=new jbutton("下月");
    previousmonth=new jbutton("上月");
    nextmonth.addactionlistener(this);
    previousmonth.addactionlistener(this);
    jpanel pnorth=new jpanel(),
    psouth=new jpanel();
    pnorth.add(choiceyear);
    pnorth.add(showyear);
    pnorth.add(choicemonth);
    pnorth.add(showmonth);
    pnorth.add(previousmonth);
    pnorth.add (nextmonth);
    psouth.add(showmessage);
    add(pcenter,borderlayout.center);
    add(pnorth,borderlayout.north);
    add(psouth,borderlayout.south);
    setyearandmonth(year,month);
    setdefaultcloseoperation(dispose_on_close);
  }
public void setyearandmonth(int y,int m){
  calendar.setyear(y);
  calendar.setmonth(m);
  string day[]=calendar.getcalendar();
  for(int i=0;i<42;i++)
    labelday[i].settext(day[i]);
  simpledateformat df = new simpledateformat("yyyy年mm月dd日 eeee");//设置日期格式
  showmessage.settext("系统时间:"+df.format(new date()));
}
//事件动作
public void actionperformed(actionevent e){
  if(e.getsource()==nextmonth){
    month=month +1;
    if(month>12)
      month=1;
    calendar.setmonth(month);
    choicemonth.setselecteditem(month);
    string day[]=calendar.getcalendar();
    for(int i=0;i<42;i++){
      labelday[i].settext(day[i]);
    }
  }
  else if(e.getsource()==previousmonth){
    month=month-1;
    if(month<1)
      month=12;
    calendar.setmonth(month);
    choicemonth.setselecteditem(month);
    string day[]=calendar.getcalendar();
    for(int i=0;i<42;i++){
      labelday[i].settext(day[i]);
    }
  }
  //选择年份
  else if (e.getsource()==choiceyear){
    calendar.setyear((integer) choiceyear.getselecteditem());
    string day[]=calendar.getcalendar();
    for(int i=0;i<42;i++){
      labelday[i].settext(day[i]);
      }
    }
  //选择月份
  else if (e.getsource()==choicemonth){
    calendar.setmonth((integer) choicemonth.getselecteditem());
    string day[]=calendar.getcalendar();
    for(int i=0;i<42;i++){
        labelday[i].settext(day[i]);
    }
  }
//  showmessage.settext("日历:"+calendar.getyear()+"年"+calendar.getmonth()+"月");
}
}

calendarbean.java

?
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
32
33
34
35
36
37
38
39
import java.util.calendar;
public class calendarbean {
 string day[];
 int year = 2005,month=0;
 public void setyear(int year){
   this.year=year;
 }
 public int getyear(){
   return year;
 }
 public void setmonth(int month){
   this.month=month;
 }
 public int getmonth(){
   return month;
 }
 public string[] getcalendar(){
   string a[]=new string[42];
   calendar 日历=calendar.getinstance();
   日历.set(year,month-1,1);
   int 星期几=日历.get(calendar.day_of_week)-1;
   int day=0;
   if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
     day=31;
   if(month==4||month==6||month==9||month==11)
     day=30;
   if(month==2){
     if(((year%4==0)&&(year%100!=0))||(year%400==0))
       day=29;
     else
       day=28;
   }
   for(int i=星期几,n=1;i<星期几+day;i++){
     a[i]=string.valueof(n);
     n++;
   }
   return a;
 }
}

calendarmainclass.java

?
1
2
3
4
5
6
7
8
9
10
public class calendarmainclass{
  public static void main(string args[])
  {
    calendarframe frame = new calendarframe();
    frame.setbounds(100,100,360,300);
    frame.settitle("java日历");
    frame.setvisible(true);
    frame.setyearandmonth(1990,1);//设置日历初始值为1990年1月
  }
}

ps:这里再为大家推荐几款时间及日期相关工具供大家参考使用:

unix时间戳(timestamp)转换工具:https://tool.zzvips.com/t/timestamp/

在线秒表计时器:https://tool.zzvips.com/t/miaobiao/

在线万年历:https://tool.zzvips.com/t/wannianli/

希望本文所述对大家java程序设计有所帮助。

原文链接:https://blog.csdn.net/C_jian/article/details/50513386