真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java代碼編寫日歷 日歷編程java代碼

Java編寫程序,輸入年份,輸出本年度各月份日歷

寫了個(gè)簡(jiǎn)明的,

成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比烏蘭網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式烏蘭網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋烏蘭地區(qū)。費(fèi)用合理售后完善,10余年實(shí)體公司更值得信賴。

import java.util.Calendar;

import java.util.Scanner;

public class Test {

static public void main(String 參數(shù)[]){

Calendar c = Calendar.getInstance();

Scanner sc = new Scanner(System.in);

System.out.println("請(qǐng)輸入年份:");

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);

}

}

}

=======

請(qǐng)輸入年份:

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編寫日歷

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.GridLayout;

import java.awt.SystemColor;

import java.awt.event.ActionEvent;

import java.awt.event.KeyEvent;

import java.awt.event.MouseEvent;

import java.util.Calendar;

import java.util.GregorianCalendar;

import java.util.Locale;

import java.util.Date;

import java.util.StringTokenizer;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.JToggleButton;

import javax.swing.SwingConstants;

import javax.swing.UIManager;

/**

* pTitle: Swing日歷/p

* pDescription: 操作日期/p

* @author duxu2004

* @version 1.0.1

*/

class JCalendar extends JPanel{

//動(dòng)態(tài)表示年月日

private int year=0;

private int month=0;

private int day=0;

//主面板

private JPanel Main = new JPanel();

//日面板

private JPanel jPanelDay = new JPanel();

//月面板

private JPanel jPanelMonth = new JPanel();

//年的輸入位置

private JTextField Year = new JTextField();

//月的輸入位置

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();

//中國時(shí)區(qū),以后可以從這里擴(kuò)展可以設(shè)置時(shí)區(qū)的功能

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();

//標(biāo)示

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();

//當(dāng)前選擇的天數(shù)按鈕

private JToggleButton cur=null;

//月份天數(shù)數(shù)組,用來取得當(dāng)月有多少天

// 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};

//空日期構(gòu)造函數(shù)

public JCalendar() {

try {

jbInit();

}

catch(Exception e) {

e.printStackTrace();

}

}

//帶日期設(shè)置的構(gòu)造函數(shù)

public JCalendar(int year, int month, int day) {

cal.set(year, month, day);

try {

jbInit();

}

catch (Exception e) {

e.printStackTrace();

}

}

//帶日歷輸入的構(gòu)造函數(shù)

public JCalendar(GregorianCalendar calendar) {

cal=calendar;

try {

jbInit();

}

catch (Exception e) {

e.printStackTrace();

}

}

//帶日期輸入的構(gòu)造函數(shù)

public JCalendar(Date date) {

cal.setTime(date);

try {

jbInit();

}

catch (Exception e) {

e.printStackTrace();

}

}

//初始化組件

private void jbInit() throws Exception {

//初始化年、月、日

iniCalender();

this.setLayout(new BorderLayout());

this.setBorder(BorderFactory.createRaisedBevelBorder());

this.setMaximumSize(new Dimension(200, 200));

this.setMinimumSize(new Dimension(200, 200));

this.setPreferredSize(new Dimension(200, 200));

Main.setLayout(new BorderLayout());

Main.setBackground(SystemColor.info);

Main.setBorder(null);

Out.setBackground(Color.lightGray);

Out.setHorizontalAlignment(SwingConstants.CENTER);

Out.setMaximumSize(new Dimension(100, 19));

Out.setMinimumSize(new Dimension(100, 19));

Out.setPreferredSize(new Dimension(100, 19));

jLabel1.setForeground(Color.red);

jLabel1.setHorizontalAlignment(SwingConstants.CENTER);

jLabel1.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel1.setText("日");

jLabel2.setForeground(Color.blue);

jLabel2.setHorizontalAlignment(SwingConstants.CENTER);

jLabel2.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel2.setText("六");

jLabel3.setHorizontalAlignment(SwingConstants.CENTER);

jLabel3.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel3.setText("五");

jLabel4.setHorizontalAlignment(SwingConstants.CENTER);

jLabel4.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel4.setText("四");

jLabel5.setHorizontalAlignment(SwingConstants.CENTER);

jLabel5.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel5.setText("三");

jLabel6.setBorder(null);

jLabel6.setHorizontalAlignment(SwingConstants.CENTER);

jLabel6.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel6.setText("二");

jLabel7.setBackground(Color.lightGray);

jLabel7.setForeground(Color.black);

jLabel7.setBorder(null);

jLabel7.setHorizontalAlignment(SwingConstants.CENTER);

jLabel7.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel7.setText("一");

weekPanel.setBackground(UIManager.getColor("InternalFrame.activeTitleGradient"));

weekPanel.setBorder(BorderFactory.createEtchedBorder());

weekPanel.setLayout(new GridLayout(1,7));

weekPanel.add(jLabel1, null);

weekPanel.add(jLabel7, null);

weekPanel.add(jLabel6, null);

weekPanel.add(jLabel5, null);

weekPanel.add(jLabel4, null);

weekPanel.add(jLabel3, null);

weekPanel.add(jLabel2, null);

MonthUp.setAlignmentX((float) 0.0);

MonthUp.setActionMap(null);

jPanelMonth.setBackground(SystemColor.info);

jPanelMonth.setLayout(new BorderLayout());

jPanelMonth.setBorder(BorderFactory.createEtchedBorder());

Month.setBorder(null);

Month.setHorizontalAlignment(SwingConstants.CENTER);

Month.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(MouseEvent e) {

Month_mouseClicked(e);

}

});

Month.addKeyListener(new java.awt.event.KeyAdapter() {

public void keyPressed(KeyEvent e) {

Month_keyPressed(e);

}

});

MonthDown.setBorder(null);

MonthDown.setText("\u25C4");

MonthDown.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

MonthDown_actionPerformed(e);

}

});

MonthUp.setBorder(null);

MonthUp.setText("\u25BA");

MonthUp.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

MonthUp_actionPerformed(e);

}

});

jPanelButton.setLayout(null);

jPanelButton.setBorder(null);

jPanelButton.addComponentListener(new java.awt.event.ComponentAdapter() {

public void componentResized(java.awt.event.ComponentEvent evt) {

jPanelButtonComponentResized(evt);

}

});

Year.setBorder(BorderFactory.createEtchedBorder());

Year.setMaximumSize(new Dimension(80, 25));

Year.setMinimumSize(new Dimension(80, 25));

Year.setPreferredSize(new Dimension(80, 25));

Year.setHorizontalAlignment(SwingConstants.CENTER);

Year.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(MouseEvent e) {

Year_mouseClicked(e);

}

});

Year.addKeyListener(new java.awt.event.KeyAdapter() {

public void keyPressed(KeyEvent e) {

Year_keyPressed(e);

}

});

YearDown.setBorder(null);

YearDown.setMaximumSize(new Dimension(16, 16));

YearDown.setMinimumSize(new Dimension(16, 16));

YearDown.setPreferredSize(new Dimension(16, 16));

YearDown.setSize(new Dimension(16, 16));

YearDown.setText("▼");

YearDown.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

YearDown_actionPerformed(e);

}

});

YearUp.setBorder(null);

YearUp.setMaximumSize(new Dimension(16, 16));

YearUp.setMinimumSize(new Dimension(16, 16));

YearUp.setPreferredSize(new Dimension(16, 16));

YearUp.setSize(new Dimension(16, 16));

YearUp.setText("▲");

YearUp.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

YearUp_actionPerformed(e);

}

});

jPanelDay.setLayout(new BorderLayout());

Days.setLayout(new GridLayout(6,7));

Days.setBackground(SystemColor.info);

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

days[i]=new JToggleButton();

days[i].setBorder(null);

days[i].setBackground(SystemColor.info);

days[i].setHorizontalAlignment(SwingConstants.CENTER);

days[i].setHorizontalTextPosition(SwingConstants.CENTER);

//days[i].setSize(l,l);

days[i].addActionListener(new java.awt.event.ActionListener(){

public void actionPerformed(ActionEvent e) {

day=Integer.parseInt(((JToggleButton)e.getSource()).getText());

showDate();

showDays();

}

});

Days.add(days[i]);

}

this.add(Main, BorderLayout.NORTH);

this.add(jPanelDay, BorderLayout.CENTER);

this.add(jPanelMonth, BorderLayout.SOUTH);

Main.add(Year, BorderLayout.CENTER);

Main.add(Out, BorderLayout.WEST);

Main.add(jPanelButton, BorderLayout.EAST);

jPanelButton.add(YearUp);

jPanelButton.add(YearDown);

jPanelDay.add(weekPanel,BorderLayout.NORTH);

jPanelDay.add(Days, BorderLayout.CENTER);

jPanelMonth.add(Month, BorderLayout.CENTER);

jPanelMonth.add(MonthDown, BorderLayout.WEST);

jPanelMonth.add(MonthUp, BorderLayout.EAST);

showMonth();

showYear();

showDate();

showDays();

}

//自定義重畫年選擇面板

void jPanelButtonComponentResized(java.awt.event.ComponentEvent evt){

YearUp.setLocation(0,0);

YearDown.setLocation(0,YearUp.getHeight());

jPanelButton.setSize(YearUp.getWidth(),YearUp.getHeight()*2);

jPanelButton.setPreferredSize(new Dimension(YearUp.getWidth(),YearUp.getHeight()*2));

jPanelButton.updateUI();

}

//測(cè)試用

public static void main(String[] args){

JFrame f=new JFrame();

f.setContentPane(new JCalendar());

f.pack();

//f.setResizable(false);

f.show();

}

//增加年份

void YearUp_actionPerformed(ActionEvent e) {

year++;

showYear();

showDate();

showDays();

}

//減少年份

void YearDown_actionPerformed(ActionEvent e) {

year--;

showYear();

showDate();

showDays();

}

//減少月份

void MonthDown_actionPerformed(ActionEvent e) {

month--;

if(month0) {

month = 11;

year--;

showYear();

}

showMonth();

showDate();

showDays();

}

//增加月份

void MonthUp_actionPerformed(ActionEvent e) {

month++;

if(month==12) {

month=0;

year++;

showYear();

}

showMonth();

showDate();

showDays();

}

//初始化年月日

void iniCalender(){

year=cal.get(Calendar.YEAR);

month=cal.get(Calendar.MONTH);

day=cal.get(Calendar.DAY_OF_MONTH);

}

//刷新月份

void showMonth(){

Month.setText(Integer.toString(month+1)+"月");

}

//刷新年份

void showYear(){

Year.setText(Integer.toString(year)+"年");

}

//刷新日期

void showDate(){

Out.setText(Integer.toString(year)+"-"+Integer.toString(month+1)+"-"+Integer.toString(day));

}

//重畫天數(shù)選擇面板

void showDays() {

cal.set(year,month,1);

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

int n=mm[month];

if(cal.isLeapYear(year)month==1) n++;

int i=0;

for(;ifirstDayOfWeek-1;i++){

days[i].setEnabled(false);

days[i].setSelected(false);

days[i].setText("");

}

int d=1;

for(;d=n;d++){

days[i].setText(Integer.toString(d));

days[i].setEnabled(true);

if(d==day) days[i].setSelected(true);

else days[i].setSelected(false);;

i++;

}

for(;i42;i++){

days[i].setEnabled(false);

days[i].setSelected(false);

days[i].setText("");

}

}

//單擊年份面板選擇整個(gè)年份字符串

void SelectionYear(){

Year.setSelectionStart(0);

Year.setSelectionEnd(Year.getText().length());

}

//單擊月份面板選擇整個(gè)月份字符串

void SelectionMonth(){

Month.setSelectionStart(0);

Month.setSelectionEnd(Month.getText().length());

}

//月份面板響應(yīng)鼠標(biāo)單擊事件

void Month_mouseClicked(MouseEvent e) {

//SelectionMonth();

inputMonth();

}

//檢驗(yàn)輸入的月份

void inputMonth(){

String s;

if(Month.getText().endsWith("月"))

{

s=Month.getText().substring(0,Month.getText().length()-1);

}

else s=Month.getText();

month=Integer.parseInt(s)-1;

this.showMe();

}

//月份面板鍵盤敲擊事件響應(yīng)

void Month_keyPressed(KeyEvent e) {

if(e.getKeyChar()==10)

inputMonth();

}

//年份面板響應(yīng)鼠標(biāo)單擊事件

void Year_mouseClicked(MouseEvent e) {

//SelectionYear();

inputYear();

}

//年份鍵盤敲擊事件響應(yīng)

void Year_keyPressed(KeyEvent e) {

//System.out.print(new Integer(e.getKeyChar()).byteValue());

if(e.getKeyChar()==10)

inputYear();

}

//檢驗(yàn)輸入的年份字符串

void inputYear() {

String s;

if(Year.getText().endsWith("年"))

{

s=Year.getText().substring(0,Year.getText().length()-1);

}

else s=Year.getText();

year=Integer.parseInt(s);

this.showMe();

}

//以字符串形式返回日期,yyyy-mm-dd

public String getDate(){return Out.getText();}

//以字符串形式輸入日期,yyyy-mm-dd

public void setDate(String date){

if(date!=null){

StringTokenizer f = new StringTokenizer(date, "-");

if(f.hasMoreTokens())

year = Integer.parseInt(f.nextToken());

if(f.hasMoreTokens())

month = Integer.parseInt(f.nextToken());

if(f.hasMoreTokens())

day = Integer.parseInt(f.nextToken());

cal.set(year,month,day);

}

this.showMe();

}

//以日期對(duì)象形式輸入日期

public void setTime(Date date){

cal.setTime(date);

this.iniCalender();

this.showMe();

}

//返回日期對(duì)象

public Date getTime(){return cal.getTime();}

//返回當(dāng)前的日

public int getDay() {

return day;

}

//設(shè)置當(dāng)前的日

public void setDay(int day) {

this.day = day;

cal.set(this.year,this.month,this.day);

this.showMe();

}

//設(shè)置當(dāng)前的年

public void setYear(int year) {

this.year = year;

cal.set(this.year,this.month,this.day);

this.showMe();

}

//返回當(dāng)前的年

public int getYear() {

return year;

}

//返回當(dāng)前的月

public int getMonth() {

return month;

}

//設(shè)置當(dāng)前的月

public void setMonth(int month) {

this.month = month;

cal.set(this.year,this.month,this.day);

this.showMe();

}

//刷新

public void showMe(){

this.showDays();

this.showMonth();

this.showYear();

this.showDate();

}

}

public class TestJCalendar {

public static void main(String[] args) {

JFrame f=new JFrame();

f.setContentPane(new JCalendar());

f.pack();

//f.setResizable(false);

f.show();

}

}

java語言編寫日歷

import java.util.*;

public class calendar

{

public static void main(String[] args)

{

new calendar().makeCalendar();

}

public void makeCalendar()

{

int i;

int j;

int year = 0;

int month = 0;

int week = 0;

int totalDay = 0;

Scanner scanner = new Scanner(System.in);

Calendar ca = Calendar.getInstance();

printAsterisk();

System.out.print("歡 迎 使 用 萬 年 歷");

printAsterisk();

System.out.print("\n\n請(qǐng)輸入年份:");

year = scanner.nextInt();

System.out.print("\n\n請(qǐng)輸入月份:");

month = scanner.nextInt() ;

ca.set(year, month - 1,1);

week = ca.get(Calendar.DAY_OF_WEEK)-1;//獲取輸入月第一天是星期幾

if( month != 2)

totalDay = calculatetotalDay(year , month);

else

{

if(judgeLeap_year(year))

totalDay = calculatetotalDay(year , month) + 1;//如果是閏年,加一天

else

totalDay = calculatetotalDay(year , month);

}

System.out.println("\n\n\n星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");

for(i = 0;i week;i++)

{

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

}

for( i = 1; i (8 - week);i++)

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

for( i = (8 - week),j = 0;i = totalDay;i++,j++)

{

if(j % 7 == 0)

System.out.println();

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

}

}

public void printAsterisk()

{

int i;

for( i = 0 ; i 18;i++)

System.out.print("*");

}

//獲取輸入月的天數(shù)

public int calculatetotalDay(int year ,int month)

{

int result = 0;

switch (month)

{

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12: result = 31;

break;

case 4:

case 6:

case 9:

case 11:result = 30;

break;

default:result = 28;

break;

}

return result;

}

//判斷閏年

public boolean judgeLeap_year(int year)

{

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

return true;

else

return false;

}

}

怎么用java寫日歷

以下是兩個(gè)類,請(qǐng)樓主分別存成兩個(gè)java文件:

其中

MainFrame.java是顯示日歷程序,Clock.java是日歷計(jì)算程序。編譯后運(yùn)行MainFrame這個(gè)類即可。

1.MainFrame.java

---

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.GridLayout;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.sql.Date;

import java.util.Calendar;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

public class MainFrame extends JFrame {

/** *//**

*

*/

private static final long serialVersionUID = 1L;

JPanel panel = new JPanel(new BorderLayout());

JPanel panel1 = new JPanel();

JPanel panel2 = new JPanel(new GridLayout(7, 7));

JPanel panel3 = new JPanel();

JLabel[] label = new JLabel[49];

JLabel y_label = new JLabel("年份");

JLabel m_label = new JLabel("月份");

JComboBox com1 = new JComboBox();

JComboBox com2 = new JComboBox();

int re_year, re_month;

int x_size, y_size;

String year_num;

Calendar now = Calendar.getInstance(); // 實(shí)例化Calendar

MainFrame() {

super("萬年歷");

setSize(300, 350);

x_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth());

y_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight());

setLocation((x_size - 300) / 2, (y_size - 350) / 2);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

panel1.add(y_label);

panel1.add(com1);

panel1.add(m_label);

panel1.add(com2);

for (int i = 0; i 49; i++) {

label[i] = new JLabel("", JLabel.CENTER);// 將顯示的字符設(shè)置為居中

panel2.add(label[i]);

}

panel3.add(new Clock(this));

panel.add(panel1, BorderLayout.NORTH);

panel.add(panel2, BorderLayout.CENTER);

panel.add(panel3, BorderLayout.SOUTH);

panel.setBackground(Color.white);

panel1.setBackground(Color.white);

panel2.setBackground(Color.white);

panel3.setBackground(Color.white);

Init();

com1.addActionListener(new ClockAction());

com2.addActionListener(new ClockAction());

setContentPane(panel);

setVisible(true);

setResizable(false);

}

class ClockAction implements ActionListener {

public void actionPerformed(ActionEvent arg0) {

int c_year, c_month, c_week;

c_year = Integer.parseInt(com1.getSelectedItem().toString()); // 得到當(dāng)前所選年份

c_month = Integer.parseInt(com2.getSelectedItem().toString()) - 1; // 得到當(dāng)前月份,并減1,計(jì)算機(jī)中的月為0-11

c_week = use(c_year, c_month); // 調(diào)用函數(shù)use,得到星期幾

Resetday(c_week, c_year, c_month); // 調(diào)用函數(shù)Resetday

}

}

public void Init() {

int year, month_num, first_day_num;

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

for (int i = 0; i 7; i++) {

label[i].setText(log[i]);

}

for (int i = 0; i 49; i = i + 7) {

label[i].setForeground(Color.red); // 將星期日的日期設(shè)置為紅色

}

for (int i = 6; i 49; i = i + 7) {

label[i].setForeground(Color.green);// 將星期六的日期設(shè)置為綠色

}

for (int i = 1; i 10000; i++) {

com1.addItem("" + i);

}

for (int i = 1; i 13; i++) {

com2.addItem("" + i);

}

month_num = (int) (now.get(Calendar.MONTH)); // 得到當(dāng)前時(shí)間的月份

year = (int) (now.get(Calendar.YEAR)); // 得到當(dāng)前時(shí)間的年份

com1.setSelectedIndex(year - 1); // 設(shè)置下拉列表顯示為當(dāng)前年

com2.setSelectedIndex(month_num); // 設(shè)置下拉列表顯示為當(dāng)前月

first_day_num = use(year, month_num);

Resetday(first_day_num, year, month_num);

}

public int use(int reyear, int remonth) {

int week_num;

now.set(reyear, remonth, 1); // 設(shè)置時(shí)間為所要查詢的年月的第一天

week_num = (int) (now.get(Calendar.DAY_OF_WEEK));// 得到第一天的星期

return week_num;

}

@SuppressWarnings("deprecation")

public void Resetday(int week_log, int year_log, int month_log) {

int month_day_score; // 存儲(chǔ)月份的天數(shù)

int count;

month_day_score = 0;

count = 1;

Date date = new Date(year_log, month_log + 1, 1); // now

Calendar cal = Calendar.getInstance();

cal.setTime(date);

cal.add(Calendar.MONTH, -1); // 前個(gè)月

month_day_score = cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 最后一天

for (int i = 7; i 49; i++) { // 初始化標(biāo)簽

label[i].setText("");

}

week_log = week_log + 6; // 將星期數(shù)加6,使顯示正確

month_day_score = month_day_score + week_log;

for (int i = week_log; i month_day_score; i++, count++) {

label[i].setText(count + "");

}

}

public static void main(String[] args) {

JFrame.setDefaultLookAndFeelDecorated(true);

new MainFrame();

}

}

2.Clock.java

-----

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.GridLayout;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.sql.Date;

import java.util.Calendar;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

//顯示時(shí)間的類:Clock

/** *//**

* Clock.java

* Summary 數(shù)字時(shí)間顯示

* Created on

* @author

* remark

*/

import java.awt.Canvas;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.text.SimpleDateFormat;

import java.util.Calendar;

class Clock extends Canvas implements Runnable{

/** *//**

*

*/

private static final long serialVersionUID = 3660124045489727166L;

MainFrame mf;

Thread t;

String time;

public Clock(MainFrame mf){

this.mf=mf;

setSize(280,40);

setBackground(Color.white);

t=new Thread(this); //實(shí)例化線程

t.start(); //調(diào)用線程

}

public void run(){

while(true){

try{

Thread.sleep(1000); //休眠1秒鐘

}catch(InterruptedException e){

System.out.println("異常");

}

this.repaint(100);

}

}

public void paint(Graphics g){

Font f=new Font("宋體",Font.BOLD,16);

SimpleDateFormat SDF=new SimpleDateFormat("yyyy'年'MM'月'dd'日'HH:mm:ss");//格式化時(shí)間顯示類型

Calendar now=Calendar.getInstance();

time=SDF.format(now.getTime()); //得到當(dāng)前日期和時(shí)間

g.setFont(f);

g.setColor(Color.orange);

g.drawString(time,45,25);

}

}

如何用JAVA編寫出一個(gè)簡(jiǎn)單的日歷

import javax.swing.JOptionPane;

public class NewClass{

public static void main(String[] args){

int year,month;

Calender cal=new Calender(2008,10);

cal.showCalender();

year=Integer.parseInt(JOptionPane.showInputDialog("Year:"));

month=Integer.parseInt(JOptionPane.showInputDialog("Month:"));

cal.setYear(year);

cal.setMonth(month);

cal.showCalender();

}

}

class Calender{

private int year,month;

public Calender(){

year=0;

month=1;

}

public Calender(int year){

this.year=year;

month=1;

}

public Calender(int year,int month){

this.year=year;

if(month12)

this.month=month%12;

else

this.month=month;

}

public void setYear(int year){

this.year=year;

}

public void setMonth(int month){

if(month12)

this.month=month%12;

else

this.month=month;

}

private int dayOfMonth(){

int days=0;

switch(month){

case 1:days=31;break;

case 2:{

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

days=29;

else

days=28;

break;

}

case 3:days=31;break;

case 4:days=30;break;

case 5:days=31;break;

case 6:days=30;break;

case 7:days=31;break;

case 8:days=31;break;

case 9:days=30;break;

case 10:days=31;break;

case 11:days=30;break;

case 12:days=31;break;

default:

days=0;

}

return days;

}

private int dayOfWeek(){

int Y=year;

int M=month;

int D=1;

int A;

A = Y0?(5+(Y+1)+(Y-1)/4-(Y-1)/100+(Y-1)/400)%7:(5+Y+Y/4-Y/100+Y/400)%7;

A = M2?(A+2*(M+1)+3*(M+1)/5)%7:(A+2*(M+2)+3*(M+2)/5)%7;

if (((Y%4 == 0 Y%100 != 0)|| Y%400 == 0) M2) A =(A+1)%7;

A=(A+D)%7;

return A;

}

public void showCalender(){

String str=new String();

str=" ";

str+=year+"年"+month+"月";

str+="\n\n";

str+="日 一 二 三 四 五 六\n";

int week=this.dayOfWeek();

for(int i=0,j=1;i7;i++){

if(iweek)

str+=" ";

else{

str+=" "+j+" ";

j++;

}

}

str+="\n";

end:

for(int i=7-week+1;i=this.dayOfMonth();){

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

if(i10)

str+=" "+i+" ";

else

str+=i+" ";

i++;

if(ithis.dayOfMonth())

break end;

}

str+="\n";

}

JOptionPane.showMessageDialog(null,str);

}

}

用一個(gè)類來實(shí)現(xiàn)

java編寫日歷

我給你貼上我在java核心技術(shù)中看到的代碼吧,當(dāng)然沒有輸入年份和月份,是按照當(dāng)前時(shí)間創(chuàng)建的,寫有我寫的注釋,應(yīng)該能看的懂

/*

* 2012年5月13日10:37:58

* 日歷程序

* Function:

* 顯示當(dāng)前月份的日歷

* 總結(jié)

* 1. 0-11分別代表1-12月

* 1-7分別代表周日-周六

* 2. 使用GregorianCalendar對(duì)象的get方法(參數(shù))獲取月,日,年等信息

* 3.

*/

import java.text.DateFormatSymbols;

import java.util.*;

public class CalendarTest {

public static void main(String[] args) {

//construct d as current date構(gòu)造一個(gè)日期

GregorianCalendar d = new GregorianCalendar();

//獲取今天是這個(gè)月的第幾天

int today = d.get(Calendar.DAY_OF_MONTH); //Calendar.DAY_OF_MONTH作為參數(shù)調(diào)用,得到今天是這個(gè)月的第幾天

int month = d.get(Calendar.MONTH); //月份

d.set(Calendar.DAY_OF_MONTH, 1); //設(shè)置d的日期是本月的1號(hào)

int weekDay = d.get(Calendar.DAY_OF_WEEK); //獲取當(dāng)天位于本星期的第幾天,也就確定了星期幾,值的范圍是1-7

int firstDayOfWeek = d.getFirstDayOfWeek(); //獲取一星期的第一天,我們得到的是Calendar.SUNDAY,因?yàn)槲覀円恍瞧诘牡谝惶焓侵苋?/p>

int indent = 0; //為了定位本月第一天,定義索引

while (weekDay != firstDayOfWeek) {

//注意,月份用0-11代表1-12月,為了清晰起見,使用常量代替,下面獲取月份得到的實(shí)際是當(dāng)前月-1的值,所以我們要加1

//System.out.printf("當(dāng)前星期的第%d天,位于當(dāng)月的第%d天, 現(xiàn)在是%d月\n",

// weekDay, d.get(Calendar.DAY_OF_MONTH), d.get(Calendar.MONTH)+1); //Test Code

indent++;//縮進(jìn)個(gè)數(shù)+1

d.add(Calendar.DAY_OF_MONTH, -1);//當(dāng)前天數(shù)-1,如果現(xiàn)在是1號(hào),則執(zhí)行本條代碼后,時(shí)間變?yōu)樯弦粋€(gè)月最后一天

weekDay = d.get(Calendar.DAY_OF_WEEK); //重新獲取當(dāng)天位于本星期的第幾天

}

String[] weekDayNames = new DateFormatSymbols().getShortWeekdays();//獲取簡(jiǎn)短形式的星期字符串?dāng)?shù)組

//System.out.println(weekDayNames.length);getShortWeekdays()得到的數(shù)組的長度是8,下標(biāo)為0的是沒有值1為星期日...7為星期六

//注釋代碼1

//Java核心技術(shù)的代碼

/*

do {

//System.out.printf("%4s", weekDayNames[weekDay]); //經(jīng)過上面定義索引,weekDay代表的是本星期日

d.add(Calendar.DAY_OF_MONTH, 1); //天數(shù)加1

weekDay = d.get(Calendar.DAY_OF_WEEK); //重新獲得weekDay的值

} while (weekDay != firstDayOfWeek); //當(dāng)循環(huán)完一個(gè)星期后,這里判斷不成立,退出循環(huán)

*/

//我寫的代碼,替換上面注釋代碼1

for (int i=1; iweekDayNames.length; i++)//打印星期標(biāo)題

System.out.printf("%3s\t", weekDayNames[i]);//引號(hào)內(nèi)是一個(gè)全角的空格,因?yàn)槭侵形陌?,不是書上英文環(huán)境,中文和空格對(duì)于不上,這里我們用\t解決

//System.out.printf("%3s ", weekDayNames[i]); //方式2

System.out.println();//換行

for (int i=1; i=indent; i++)//確定一星期的一天位置,利用上面indent

System.out.printf("\t");//如用方式2,則這里內(nèi)容是四個(gè)全角空格

//實(shí)現(xiàn)輸出日期

d.set(Calendar.MONTH, month);

d.set(Calendar.DAY_OF_MONTH, 1);

do {

//print day

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

System.out.printf("%3d", day);

if (day == today)

System.out.print("*");

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

d.add(Calendar.DATE, 1);//天數(shù)加1

weekDay = d.get(Calendar.DAY_OF_WEEK);//刷新weekDay

if (weekDay == firstDayOfWeek) //如果這天等于星期天則換行

System.out.println();

} while (d.get(Calendar.MONTH) == month);

}

}


當(dāng)前文章:java代碼編寫日歷 日歷編程java代碼
網(wǎng)站網(wǎng)址:http://weahome.cn/article/hhhcpd.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部