评论

收藏

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

编程语言 编程语言 发布于:2021-09-18 17:27 | 阅读数:400 | 评论:0

这篇文章主要介绍了Java实现的日历功能,结合完整实例形式分析了Java日历功能相关的日期时间获取、计算、显示等操作技巧,需要的朋友可以参考下
本文实例讲述了java实现的日历功能。分享给大家供大家参考,具体如下:
应用名称:java日历
用到的知识:java gui编程,日期操作
开发环境:win8+eclipse+jdk1.8
功能说明:一个很简单的万年历,可以选择年份和月份,也可以用按钮翻页,日历会实时更新日期,最下方会显示当前操作系统的时间。
效果图:
DSC0000.png

源代码:
calendarframe.java
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
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
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

关注下面的标签,发现更多相似文章