资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

日历java程序代码 java实现简单日历代码

java程序关于日历代码的解读

类CalendarPrinter有两个主要的方法printCal()和printOut(),printCal()打印你输入年份的每一个月份,printOut()打印每一个月的每一天,在printCal()方法先创建一个格里高利历法的对象,并把你输入的年份设置为该历法的年份,然后for循环打印12个月份,然后再把每个月分别设置为你输入那年的每个月,接着调用printOut()方法打印每一天,在printOut()方法中,先获得月份,然后把日期设置为该月的第一天,接着在获取这月的第一天是一周中的周几weekday,然后打印出第一行“Sun MOn Tue Wed Thu Fri Sat”,接着就进入for循环,这个for循环就是打印每个月第一天开始前的空格,接着就进入do……while循环,获得天day,如果day10,打印day的时候day前面的空格大一些,否则day前面的空格就小一些,为了排版好看,然后判断是否是周六,如果是周六就换行,然后把day+1,重新获得新的day在一周中是周几。循环一直下去,该月的天从1一直加到30或31,再加的话月份就该+1,所以do……while循环结束的条件就是进入到下一个月。最后判断该月的最后一天是不是该月的周日,如果不是就换行

创新互联建站科技有限公司专业互联网基础服务商,为您提供服务器托管高防服务器,成都IDC机房托管,成都主机托管等互联网服务。

Java编写程序,输入年份,输出本年度各月份日历

写了个简明的,

import java.util.Calendar;

import java.util.Scanner;

public class Test {

static public void main(String 参数[]){

Calendar c = Calendar.getInstance();

Scanner sc = new Scanner(System.in);

System.out.println("请输入年份:");

int year= sc.nextInt();

c.set(Calendar.YEAR, year);

c.set(Calendar.MONTH, Calendar.JANUARY);

c.set(Calendar.DAY_OF_MONTH, 1);

while(c.get(Calendar.YEAR)==year){

int wday=c.get(Calendar.DAY_OF_WEEK);

int mday=c.get(Calendar.DAY_OF_MONTH);

if(mday==1){

System.out.println("\n日\t一\t二\t三\t四\t五\t六\t第"+(c.get(Calendar.MONTH)+1)+"月");

System.out.println("---------------------------------------------------");

for(int i=0;iwday-1;i++) System.out.print(" \t");

}

System.out.print(mday+"\t");

if(wday==7) System.out.println();

c.add(Calendar.DAY_OF_YEAR, 1);

}

}

}

=======

请输入年份:

2012

日 一 二 三 四 五 六 第1月

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

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

日 一 二 三 四 五 六 第2月

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

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

日 一 二 三 四 五 六 第3月

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

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

日 一 二 三 四 五 六 第4月

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

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

日 一 二 三 四 五 六 第5月

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

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

日 一 二 三 四 五 六 第6月

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

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

日 一 二 三 四 五 六 第7月

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

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

日 一 二 三 四 五 六 第8月

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

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

日 一 二 三 四 五 六 第9月

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

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

日 一 二 三 四 五 六 第10月

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

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

日 一 二 三 四 五 六 第11月

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

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

日 一 二 三 四 五 六 第12月

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

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

关于java的日历程序

修改部分见注释

public static void main(String[] args)

{

System.out.println("   日           一          二           三           四            五           六");

Calendar rili = Calendar.getInstance();

rili.set(2011, 3, 1);

int xingqi = rili.get(Calendar.DAY_OF_WEEK) - 1;

String a[] = new String[xingqi + 30];

for (int i = 0; i  xingqi; i++)

{

a[i] = "**";

}

for (int i = xingqi, n = 1; i  xingqi + 30; i++)

{

if (n = 9)

a[i] = String.valueOf(n) + " ";

else

a[i] = String.valueOf(n);

n++;

}

//a[i]的i从0开始,你下面从1开始循,自然少了第一个

for (int i = 0; i  a.length; i++)

{

//遇到星期六应该是先打印日期,再换行,你之前是先换行,再打星期六

System.out.print("  " + a[i]);

//改了循环起始位置,此处改为i+1

if ((i + 1)% 7 == 0)

{

System.out.println(" ");

}

}

}

急需日历记事本JAVA源代码

import java.util.Calendar;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.util.Hashtable;

public class CalendarPad extends JFrame implements MouseListener

{

int year,month,day;

Hashtable hashtable;

File file;

JTextField showDay[];

JLabel title[];

Calendar 日历;

int 星期几;

NotePad notepad=null;

Month 负责改变月;

Year 负责改变年;

String 星期[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};

JPanel leftPanel,rightPanel;

public CalendarPad(int year,int month,int day)

{

leftPanel=new JPanel();

JPanel leftCenter=new JPanel();

JPanel leftNorth=new JPanel();

leftCenter.setLayout(new GridLayout(7,7));

rightPanel=new JPanel();

this.year=year;

this.month=month;

this.day=day;

负责改变年=new Year(this);

负责改变年.setYear(year);

负责改变月=new Month(this);

负责改变月.setMonth(month);

title=new JLabel[7];

showDay=new JTextField[42];

for(int j=0;j7;j++)

{

title[j]=new JLabel();

title[j].setText(星期[j]);

title[j].setBorder(BorderFactory.createRaisedBevelBorder());

leftCenter.add(title[j]);

}

title[0].setForeground(Color.red);

title[6].setForeground(Color.blue);

for(int i=0;i42;i++)

{

showDay[i]=new JTextField();

showDay[i].addMouseListener(this);

showDay[i].setEditable(false);

leftCenter.add(showDay[i]);

}

日历=Calendar.getInstance();

Box box=Box.createHorizontalBox();

box.add(负责改变年);

box.add(负责改变月);

leftNorth.add(box);

leftPanel.setLayout(new BorderLayout());

leftPanel.add(leftNorth,BorderLayout.NORTH);

leftPanel.add(leftCenter,BorderLayout.CENTER);

leftPanel.add(new Label("请在年份输入框输入所查年份(负数表示公元前),并回车确定"),

BorderLayout.SOUTH) ;

leftPanel.validate();

Container con=getContentPane();

JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,

leftPanel,rightPanel);

con.add(split,BorderLayout.CENTER);

con.validate();

hashtable=new Hashtable();

file=new File("日历记事本.txt");

if(!file.exists())

{

try{

FileOutputStream out=new FileOutputStream(file);

ObjectOutputStream objectOut=new ObjectOutputStream(out);

objectOut.writeObject(hashtable);

objectOut.close();

out.close();

}

catch(IOException e)

{

}

}

notepad=new NotePad(this);

rightPanel.add(notepad);

设置日历牌(year,month);

addWindowListener(new WindowAdapter()

{ public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

setVisible(true);

setBounds(100,50,524,285);

validate();

}

public void 设置日历牌(int year,int month)

{

日历.set(year,month-1,1);

星期几=日历.get(Calendar.DAY_OF_WEEK)-1;

if(month==1||month==2||month==3||month==5||month==7

||month==8||month==10||month==12)

{

排列号码(星期几,31);

}

else if(month==4||month==6||month==9||month==11)

{

排列号码(星期几,30);

}

else if(month==2)

{

if((year%4==0year%100!=0)||(year%400==0))

{

排列号码(星期几,29);

}

else

{

排列号码(星期几,28);

}

}

}

public void 排列号码(int 星期几,int 月天数)

{

for(int i=星期几,n=1;i星期几+月天数;i++)

{

showDay[i].setText(""+n);

if(n==day)

{

showDay[i].setForeground(Color.green);

showDay[i].setFont(new Font("TimesRoman",Font.BOLD,20));

}

else

{

showDay[i].setFont(new Font("TimesRoman",Font.BOLD,12));

showDay[i].setForeground(Color.black);

}

if(i%7==6)

{

showDay[i].setForeground(Color.blue);

}

if(i%7==0)

{

showDay[i].setForeground(Color.red);

}

n++;

}

for(int i=0;i星期几;i++)

{

showDay[i].setText("");

}

for(int i=星期几+月天数;i42;i++)

{

showDay[i].setText("");

}

}

public int getYear()

{

return year;

}

public void setYear(int y)

{

year=y;

notepad.setYear(year);

}

public int getMonth()

{

return month;

}

public void setMonth(int m)

{

month=m;

notepad.setMonth(month);

}

public int getDay()

{

return day;

}

public void setDay(int d)

{

day=d;

notepad.setDay(day);

}

public Hashtable getHashtable()

{

return hashtable;

}

public File getFile()

{

return file;

}

public void mousePressed(MouseEvent e)

{

JTextField source=(JTextField)e.getSource();

try{

day=Integer.parseInt(source.getText());

notepad.setDay(day);

notepad.设置信息条(year,month,day);

notepad.设置文本区(null);

notepad.获取日志内容(year,month,day);

}

catch(Exception ee)

{

}

}

public void mouseClicked(MouseEvent e)

{

}

public void mouseReleased(MouseEvent e)

{

}

public void mouseEntered(MouseEvent e)

{

}

public void mouseExited(MouseEvent e)

{

}

public static void main(String args[])

{

Calendar calendar=Calendar.getInstance();

int y=calendar.get(Calendar.YEAR);

int m=calendar.get(Calendar.MONTH)+1;

int d=calendar.get(Calendar.DAY_OF_MONTH);

new CalendarPad(y,m,d);

}

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Month extends Box implements ActionListener

{

int month;

JTextField showMonth=null;

JButton 下月,上月;

CalendarPad 日历;

public Month(CalendarPad 日历)

{

super(BoxLayout.X_AXIS);

this.日历=日历;

showMonth=new JTextField(2);

month=日历.getMonth();

showMonth.setEditable(false);

showMonth.setForeground(Color.blue);

showMonth.setFont(new Font("TimesRomn",Font.BOLD,16));

下月=new JButton("下月");

上月=new JButton("上月");

add(上月);

add(showMonth);

add(下月);

上月.addActionListener(this);

下月.addActionListener(this);

showMonth.setText(""+month);

}

public void setMonth(int month)

{

if(month=12month=1)

{

this.month=month;

}

else

{

this.month=1;

}

showMonth.setText(""+month);

}

public int getMonth()

{

return month;

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==上月)

{

if(month=2)

{

month=month-1;

日历.setMonth(month);

日历.设置日历牌(日历.getYear(),month);

}

else if(month==1)

{

month=12;

日历.setMonth(month);

日历.设置日历牌(日历.getYear(),month);

}

showMonth.setText(""+month);

}

else if(e.getSource()==下月)

{

if(month12)

{

month=month+1;

日历.setMonth(month);

日历.设置日历牌(日历.getYear(),month);

}

else if(month==12)

{

month=1;

日历.setMonth(month);

日历.设置日历牌(日历.getYear(),month);

}

showMonth.setText(""+month);

}

}

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

import javax.swing.event.*;

import java.io.*;

public class NotePad extends JPanel implements ActionListener

{

JTextArea text;

JButton 保存日志,删除日志;

Hashtable table;

JLabel 信息条;

int year,month,day;

File file;

CalendarPad calendar;

public NotePad(CalendarPad calendar)

{

this.calendar=calendar;

year=calendar.getYear();

month=calendar.getMonth();

day=calendar.getDay();;

table=calendar.getHashtable();

file=calendar.getFile();

信息条=new JLabel(""+year+"年"+month+"月"+day+"日",JLabel.CENTER);

信息条.setFont(new Font("TimesRoman",Font.BOLD,16));

信息条.setForeground(Color.blue);

text=new JTextArea(10,10);

保存日志=new JButton("保存日志") ;

删除日志=new JButton("删除日志") ;

保存日志.addActionListener(this);

删除日志.addActionListener(this);

setLayout(new BorderLayout());

JPanel pSouth=new JPanel();

add(信息条,BorderLayout.NORTH);

pSouth.add(保存日志);

pSouth.add(删除日志);

add(pSouth,BorderLayout.SOUTH);

add(new JScrollPane(text),BorderLayout.CENTER);

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==保存日志)

{

保存日志(year,month,day);

}

else if(e.getSource()==删除日志)

{

删除日志(year,month,day);

}

}

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 void setDay(int day)

{

this.day=day;

}

public int getDay()

{

return day;

}

public void 设置信息条(int year,int month,int day)

{

信息条.setText(""+year+"年"+month+"月"+day+"日");

}

public void 设置文本区(String s)

{

text.setText(s);

}

public void 获取日志内容(int year,int month,int day)

{

String key=""+year+""+month+""+day;

try

{

FileInputStream inOne=new FileInputStream(file);

ObjectInputStream inTwo=new ObjectInputStream(inOne);

table=(Hashtable)inTwo.readObject();

inOne.close();

inTwo.close();

}

catch(Exception ee)

{

}

if(table.containsKey(key))

{

String m=""+year+"年"+month+"月"+day+"这一天有日志记载,想看吗?";

int ok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,

JOptionPane.QUESTION_MESSAGE);

if(ok==JOptionPane.YES_OPTION)

{

text.setText((String)table.get(key));

}

else

{

text.setText("");

}

}

else

{

text.setText("无记录");

}

}

public void 保存日志(int year,int month,int day)

{

String 日志内容=text.getText();

String key=""+year+""+month+""+day;

String m=""+year+"年"+month+"月"+day+"保存日志吗?";

int ok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,

JOptionPane.QUESTION_MESSAGE);

if(ok==JOptionPane.YES_OPTION)

{

try

{

FileInputStream inOne=new FileInputStream(file);

ObjectInputStream inTwo=new ObjectInputStream(inOne);

table=(Hashtable)inTwo.readObject();

inOne.close();

inTwo.close();

table.put(key,日志内容);

FileOutputStream out=new FileOutputStream(file);

ObjectOutputStream objectOut=new ObjectOutputStream(out);

objectOut.writeObject(table);

objectOut.close();

out.close();

}

catch(Exception ee)

{

}

}

}

public void 删除日志(int year,int month,int day)

{

String key=""+year+""+month+""+day;

if(table.containsKey(key))

{

String m="删除"+year+"年"+month+"月"+day+"日的日志吗?";

int ok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,

JOptionPane.QUESTION_MESSAGE);

if(ok==JOptionPane.YES_OPTION)

{

try

{

FileInputStream inOne=new FileInputStream(file);

ObjectInputStream inTwo=new ObjectInputStream(inOne);

table=(Hashtable)inTwo.readObject();

inOne.close();

inTwo.close();

table.remove(key);

FileOutputStream out=new FileOutputStream(file);

ObjectOutputStream objectOut=new ObjectOutputStream(out);

objectOut.writeObject(table);

objectOut.close();

out.close();

text.setText(null);

}

catch(Exception ee)

{

}

}

}

else

{

String m=""+year+"年"+month+"月"+day+"无日志记录";

JOptionPane.showMessageDialog(this,m,"提示",JOptionPane.WARNING_MESSAGE);

}

}

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Year extends Box implements ActionListener

{

int year;

JTextField showYear=null;

JButton 明年,去年;

CalendarPad 日历;

public Year(CalendarPad 日历)

{

super(BoxLayout.X_AXIS);

showYear=new JTextField(4);

showYear.setForeground(Color.blue);

showYear.setFont(new Font("TimesRomn",Font.BOLD,14));

this.日历=日历;

year=日历.getYear();

明年=new JButton("下年");

去年=new JButton("上年");

add(去年);

add(showYear);

add(明年);

showYear.addActionListener(this);

去年.addActionListener(this);

明年.addActionListener(this);

}

public void setYear(int year)

{

this.year=year;

showYear.setText(""+year);

}

public int getYear()

{

return year;

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==去年)

{

year=year-1;

showYear.setText(""+year);

日历.setYear(year);

日历.设置日历牌(year,日历.getMonth());

}

else if(e.getSource()==明年)

{

year=year+1;

showYear.setText(""+year);

日历.setYear(year);

日历.设置日历牌(year,日历.getMonth());

}

else if(e.getSource()==showYear)

{

try

{

year=Integer.parseInt(showYear.getText());

showYear.setText(""+year);

日历.setYear(year);

日历.设置日历牌(year,日历.getMonth());

}

catch(NumberFormatException ee)

{

showYear.setText(""+year);

日历.setYear(year);

日历.设置日历牌(year,日历.getMonth());

}

}

}

}

希望能帮到你,以上分为4个类。。分割线以标注了

用java写个日历程序怎么写,请给出详细步骤,谢谢

随便做了一个,其实一楼网友说的判断哪些30天,是否闰年什么的没必要,看代码:

package com.baidu.calendar;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.event.ItemEvent;

import java.awt.event.ItemListener;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.border.CompoundBorder;

import javax.swing.border.EmptyBorder;

import javax.swing.border.LineBorder;

public class CalendarInterface extends JFrame {

private static final long serialVersionUID = 1L;

private JComboBox yearBox, monthBox;

private static final int YEAR_RANGE = 50; // 年份范围,即往前往后各推多少年

private JPanel topPane, contentPane;

private JPanel bottomPane, bottomTopPane, mainPane;

private JLabel dateLabel;

private boolean init = false;

private Calendar cal;

private static final Calendar NOW = Calendar.getInstance();

private static final String[] DAY_OF_WEEK = {"星期日", "星期一", "星期二",

"星期三", "星期四", "星期五", "星期六"};

private DateFormat df = new SimpleDateFormat("yyyy年MM月");

private static final Color FONT_GRAY = new Color(0xaa, 0xaa, 0xaa);

private static final int DATE_GRAY = -1;

private static final int DATE_RED = 1;

private static final int DATE_BLACK = 0;

public CalendarInterface() {

super("日历");

cal = Calendar.getInstance();

cal.setFirstDayOfWeek(Calendar.SUNDAY);

}

public void launch() {

setVisible(true);

setSize(600, 450);

setDefaultCloseOperation(EXIT_ON_CLOSE);

// 主面板

contentPane = new JPanel(new BorderLayout());

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

contentPane.setBackground(Color.WHITE);

// 顶部选择日期和月份的区域

topPane = new JPanel(new FlowLayout(FlowLayout.CENTER, 50, 0)); // 居中对齐,控件间隔50px

topPane.setBorder(new EmptyBorder(0, 0, 10, 0)); // 底部空出10px

topPane.setBackground(Color.WHITE);

// 底部显示星期条和日历列表区域

bottomPane = new JPanel(new BorderLayout());

bottomPane.setBorder(new LineBorder(Color.BLACK, 1));

// 星期条

bottomTopPane = new JPanel(new GridLayout(1, 7));

bottomTopPane.setBackground(new Color(0x55, 0x55, 0x55));

// 日期列表

mainPane = new JPanel();

mainPane.setLayout(new GridLayout(0, 7)); // 列数为7,行数自动填充

for(int i = Calendar.SUNDAY; i = Calendar.SATURDAY; i ++) {

bottomTopPane.add(createDay(DAY_OF_WEEK[i - 1]));

}

bottomPane.add(bottomTopPane, BorderLayout.NORTH); // 星期栏放在顶部

// 年份下拉列表

yearBox = new JComboBox();

int currentYear = cal.get(Calendar.YEAR);

// 年份范围为往前往后各推50年

for(int i = currentYear - YEAR_RANGE; i  currentYear + YEAR_RANGE + 1; i ++) {

yearBox.addItem(i);

}

yearBox.addItemListener(new ItemListener() {

@Override

public void itemStateChanged(ItemEvent e) {

if(ItemEvent.SELECTED == e.getStateChange()) {

reset(); // 选择后刷新布局

}

}

});

yearBox.setSelectedIndex(YEAR_RANGE); // 默认选中当前年份

String months[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"};

// 月份下拉列表

monthBox = new JComboBox(months);

monthBox.addItemListener(new ItemListener() {

@Override

public void itemStateChanged(ItemEvent e) {

if(ItemEvent.SELECTED == e.getStateChange()) {

reset(); //刷新布局

}

}

});

monthBox.setSelectedIndex(cal.get(Calendar.MONTH)); // 选择当前月份

// 显示当前年月的控件

this.dateLabel = new JLabel(df.format(cal.getTime()));

topPane.add(yearBox);

topPane.add(monthBox);

topPane.add(dateLabel);

contentPane.add(topPane, BorderLayout.NORTH);

bottomPane.add(mainPane, BorderLayout.CENTER);

contentPane.add(bottomPane, BorderLayout.CENTER);

setContentPane(contentPane);

validate();

init = true; // 初始化完成

reset(); // 刷新界面

setLocationRelativeTo(null); // 居中显示

}

public static void main(String[] args) {

new CalendarInterface().launch();

}

private void reset() { // 每次年份或月份改变后则日历重新排列

if(!init) { // 若未初始化则返回,避免设置默认年月时调用此方法出错

return;

}

int year = (Integer) yearBox.getSelectedItem();

int month = Integer.parseInt((String) monthBox.getSelectedItem());

// 将日期设置为本月第一天

cal.set(Calendar.YEAR, year);

cal.set(Calendar.MONTH, month - 1);

cal.set(Calendar.DATE, 1);

dateLabel.setText(df.format(cal.getTime())); // 显示年月

mainPane.removeAll(); // 清空日历列表

// 判断本月第一天是星期几,在第一天之前增加空的日历

int startDayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

int i;

// 先设置为本周属于上个月的几天,置灰显示

cal.add(Calendar.DATE, -(startDayOfWeek - Calendar.SUNDAY) - 1);

for(i = Calendar.SUNDAY; i  startDayOfWeek; i ++) {

cal.add(Calendar.DATE, 1);

int date = cal.get(Calendar.DATE);

mainPane.add(createDate(Integer.toString(date), DATE_GRAY));

}

i = startDayOfWeek;

// 重新初始化为本月第一天

cal.set(Calendar.MONTH, month - 1);

cal.set(Calendar.DATE, 1);

int maxDate = cal.getActualMaximum(Calendar.DATE); // 本月最多的天数(不用再去判断是否闰年了)

for(int j = 1; j = maxDate; j ++) {

cal.set(Calendar.DATE, j); // 日期一直自增,用来判断是否是今天

JLabel jlDate = null;

if(isToday()) { // 是今天则高亮显示

jlDate = createDate(Integer.toString(j), DATE_RED);

} else { // 不是今天则显示为普通颜色

jlDate = createDate(Integer.toString(j));

}

mainPane.add(jlDate);

}

// 最后一周把不属于本月的几天用灰色控件填充

int lastDay = cal.get(Calendar.DAY_OF_WEEK);

for(i = lastDay; i  Calendar.SATURDAY; i ++) {

cal.add(Calendar.DATE, 1);

int date = cal.get(Calendar.DATE);

mainPane.add(createDate(Integer.toString(date), DATE_GRAY));

}

mainPane.validate();

}

private JLabel createDate(String date) {

return createDate(date, DATE_BLACK);

}

// 创建日期控件

private JLabel createDate(String date, int dateColor) {

JLabel label = new JLabel(date, JLabel.CENTER);

CompoundBorder border = new CompoundBorder(

new LineBorder(Color.WHITE, 1),

new EmptyBorder(10, 10, 10, 10));

label.setBorder(border);

Font font = new Font("Arial", Font.BOLD, 30);

if(DATE_GRAY == dateColor) { // 如果不是本月则文字置灰

label.setForeground(FONT_GRAY);

} else if(DATE_RED == dateColor) { // 如果是今天则高亮显示

label.setForeground(Color.RED);

}

label.setFont(font);

return label;

}

// 创建星期几的横条

private JLabel createDay(String day) {

JLabel jlDay = new JLabel(day, JLabel.CENTER);

jlDay.setBorder(new EmptyBorder(5, 5, 5, 5));

jlDay.setBackground(Color.GRAY);

jlDay.setForeground(Color.WHITE);

return jlDay;

}

// 判断是否今天

private boolean isToday() {

return cal.get(Calendar.YEAR) == NOW.get(Calendar.YEAR)

 cal.get(Calendar.MONTH) == NOW.get(Calendar.MONTH)

 cal.get(Calendar.DATE) == NOW.get(Calendar.DATE);

}

}

java 日历的代码问题

首先,控制台输入肯定要是形如2013-03-06这样的格式,这是SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD")所要求的。输入2013-03会进入异常处理提示输入格式不正确。至于打印日历,你可以算出该月的第一号是星期几,然后按顺序排下去,满7天换行就是了


本文题目:日历java程序代码 java实现简单日历代码
网页URL:http://cdkjz.cn/article/hhjdsc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220