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

 更新时间:2019年02月25日 10:44:11   作者:c_jian  
这篇文章主要介绍了Java实现的日历功能,结合完整实例形式分析了Java日历功能相关的日期时间获取、计算、显示等操作技巧,需要的朋友可以参考下

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

应用名称:Java日历

用到的知识:Java GUI编程,日期操作

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

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

效果图:

源代码:

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)转换工具:
http://tools.jb51.net/code/unixtime

在线日期/天数计算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi

在线日期计算器/相差天数计算器:
http://tools.jb51.net/jisuanqi/datecalc

在线日期天数差计算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq

更多关于java相关内容感兴趣的读者可查看本站专题:《java日期与时间操作技巧汇总》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总

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

相关文章

  • Java中Map循环遍历的五种方法实现

    Java中Map循环遍历的五种方法实现

    本文主要介绍了Java中Map循环遍历的五种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • 解析mybatis-plus中的resultMap简单使用

    解析mybatis-plus中的resultMap简单使用

    mybatis-plus也只是听过,可是终究没有使用过。于是自己花几天晚上的时间研究mybatis-plus的使用。这篇文章主要介绍了mybatis-plus的resultMap简单使用,需要的朋友可以参考下
    2021-11-11
  • 深入浅析ZooKeeper的工作原理

    深入浅析ZooKeeper的工作原理

    ZooKeeper 是一个开源的分布式协调服务,由雅虎创建,是 Google Chubby 的开源实现。接下来通过本文跟大家介绍ZooKeeper的原理,小编觉得挺不错的,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • Android Java判断密码强度方法实例(强度显示)

    Android Java判断密码强度方法实例(强度显示)

    在现代社会中,密码是确保我们个人信息和账户安全的重要手段之一,为了提高密码的安全性,我们可以使用正则表达式来判断密码的强度,这篇文章主要给大家介绍了关于Android Java判断密码强度(强度显示)的相关资料,需要的朋友可以参考下
    2024-03-03
  • java中instanceof 关键字作用和实际用途详解

    java中instanceof 关键字作用和实际用途详解

    这篇文章主要介绍了java中instanceof 关键字作用和实际用途,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-04-04
  • SpringBoot使用Jwt处理跨域认证问题的教程详解

    SpringBoot使用Jwt处理跨域认证问题的教程详解

    这篇文章主要介绍了SpringBoot使用Jwt处理跨域认证问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-06-06
  • Java反射机制原理、Class获取方式以及应用场景详解

    Java反射机制原理、Class获取方式以及应用场景详解

    反射机制是JAVA的核心知识点之一,大多数框架的实现原理就是利用了反射机制,掌握反射机制会使你学习框架更加轻松高效,这篇文章主要给大家介绍了关于Java反射机制原理、Class获取方式以及应用场景的相关资料,需要的朋友可以参考下
    2022-04-04
  • java版十大排序经典算法:完整代码(3)

    java版十大排序经典算法:完整代码(3)

    优秀的文章也不少,但是Java完整版的好像不多,我把所有的写一遍巩固下,同时也真诚的希望阅读到这篇文章的小伙伴们可以自己去从头敲一遍,不要粘贴复制!希望我的文章对你有所帮助,每天进步一点点
    2021-07-07
  • 在IDEA中创建跑得起来的Springboot项目

    在IDEA中创建跑得起来的Springboot项目

    这篇文章主要介绍了在IDEA中创建跑得起来的Springboot项目的图文教程,需要的朋友可以参考下
    2018-04-04
  • java运算符实例用法总结

    java运算符实例用法总结

    在本篇文章里,我们给大家分享的是关于java运算符实例用法及实例代码,需要的朋友们参考下。
    2020-02-02

最新评论