use java编写日历

学习java中的Calendar类发现编写日历真的很有意思,要是没事就可以编个程序随便查看xxx年xxx月xx日的日历 very nice!

**CalendarBean类**
import java.util.*;
public class CalendarBean {
 String [] day; 
 int year=0,month=0;
 public void setYear(int year){
     this.year=year;
 }
 public void setMonth(int month){
     this.month=month;
 }
 public String [] getCalendar(){
     String [] a=new String[42];
     Calendar rili=Calendar.getInstance();//初始化
     rili.set(year,month-1,1);
     int weekDay=rili.get(Calendar.DAY_OF_WEEK)-1; //计算出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%400==0)||((year%4==0)&&(year%100!=0))){
             day=29;
         }
         else
             day=28;
     }
      for(int i=0;i<weekDay;i++)
         a[i]=" ";
     for(int i=weekDay,n=1;i<weekDay+day;i++) {
         a[i]=String.valueOf(n) ;
         n++;
     }  
     for(int i=weekDay+day;i<a.length;i++)
          a[i]=" ";
      return a;
 }
}

**主类**
public class Example8_16 {
    public static void main(String[] args) {
     CalendarBean cb=new CalendarBean();
      cb.setYear(2016);
      cb.setMonth(7);
      String [] a= cb.getCalendar();//返回号码的一维数组
      char [] str="日一二三四五六".toCharArray();
      for(char c:str) {     
         System.out.printf("%3c",c);
      }
      for(int i=0;i<a.length;i++) {     //输出数组a
         if(i%7==0)
             System.out.println("");  //换行
         System.out.printf("%4s",a[i]);
      }    
    }
}

运行结果截图:
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ublic class JCalendar extends JPanel{ //动态表示年月日 private int year=0; private int month=0; private int day=0; private int first = 0; //用于标志日历面板是否显示 private static boolean isShow = false; //主面板 private JPanel Main = new JPanel(); //日面板 private JPanel jPanelDay = new JPanel(); //月面板 private JPanel jPanelMonth = new JPanel(); //月的输入位置 private JTextField Month = new JTextField(); //减少月份 private JButton MonthDown = new JButton(); //增加月份 private JButton MonthUp = new JButton(); private JPanel jPanelButton = new JPanel(); //减少年份 private JButton YearDown = new JButton(); //增加年份 private JButton YearUp = new JButton(); //显示日期的位置 private JLabel Out = new JLabel(); public JLabel Out2 = new JLabel(); private Locale l=Locale.CHINESE; //主日历 private GregorianCalendar cal=new GregorianCalendar(l); //星期面板 private JPanel weekPanel=new JPanel(); //天按钮组 private JToggleButton[] days=new JToggleButton[42]; //天面板 private JPanel Days = new JPanel(); //标示 private JLabel jLabel1 = new JLabel(); private JLabel jLabel2 = new JLabel(); private JLabel jLabel3 = new JLabel(); private JLabel jLabel4 = new JLabel(); private JLabel jLabel5 = new JLabel(); private JLabel jLabel6 = new JLabel(); private JLabel jLabel7 = new JLabel(); //当前选择的天数按钮 private JToggleButton cur=null; //月份天数数组,用来取得当月有多少天 // 1 2 3 4 5 6 7 8 9 10 11 12 private int[] mm={31,28,31,30,31,30,31,31,30,31,30,31}; //空日期构造函数 public JCalendar() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //带日期设置的构造函数 public JCalendar(int year, int month, int day) { cal.set(year, month, day); try { jbInit(); } catch (Exception e) { e.printStackTrace(); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值