本文目錄一覽:
如何用java語言 寫出一個萬年曆呢? 要求自己輸入年份 自動出現月 日 以及對應的星期
如果要月曆,只要把月份循環那裡修改下,直接調用月曆方法既可
import java.text.DateFormatSymbols;
import java.util.Calendar;
import javax.swing.JOptionPane;
public class YearCalendar {
public static void main(String[] args) {
final String title = getCalTitle();
String input = JOptionPane.showInputDialog(“Please input year”);
try{
if(!input.trim().matches(“^\\d{4}$”)){
throw new NumberFormatException();
}
int year = Integer.parseInt(input.trim());
System.out.println(“——- Calendar For Year ” + year + ” —————-“);
String[] monthTitles = new DateFormatSymbols().getMonths();
for(int month = Calendar.JANUARY; month = Calendar.DECEMBER; month++){
System.out.println(“\t********** ” + monthTitles[month] + ” *********”);
System.out.println(title);
generateMonthlyCalendar(year, month);
System.out.println(“\n\n”);
}
}catch(NumberFormatException nbFmtExp){
JOptionPane.showMessageDialog(null, “Error data foramt! Date should be 4 digits only format yyyy”);
System.exit(0);
}
}
private static String getCalTitle() {
StringBuffer sb = new StringBuffer();
String[] ary = new DateFormatSymbols().getShortWeekdays();
for(int i = Calendar.SUNDAY; i = Calendar.SATURDAY; i++){
sb.append(ary[i]+ “\t”);
}
return sb.toString();
}
private static void generateMonthlyCalendar(int year, int month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DATE, 1);
int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
int i = 0;
for(i = Calendar.SUNDAY; i cal.get(Calendar.DAY_OF_WEEK); i++){
System.out.print(” \t”);
}
while(cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY){
System.out.print(cal.get(Calendar.DATE) + “\t”);
cal.add(Calendar.DATE, 1);
}
int weekDay = Calendar.SATURDAY;
int day = cal.get(Calendar.DATE);
while(day = maxDay){
if(weekDay == Calendar.SATURDAY){
System.out.println();
weekDay = Calendar.SUNDAY;
}else{
weekDay++;
}
System.out.print(day++ + “\t”);
}
}
}
——————————–JDK 1.5結果
——- Calendar For Year 2011 —————-
********** January *********
Sun Mon Tue Wed Thu Fri Sat
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
********** February *********
Sun Mon Tue Wed Thu Fri Sat
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
********** March *********
Sun Mon Tue Wed Thu Fri Sat
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
********** April *********
Sun Mon Tue Wed Thu Fri Sat
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
********** May *********
Sun Mon Tue Wed Thu Fri Sat
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
********** June *********
Sun Mon Tue Wed Thu Fri Sat
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
********** July *********
Sun Mon Tue Wed Thu Fri Sat
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
********** August *********
Sun Mon Tue Wed Thu Fri Sat
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
********** September *********
Sun Mon Tue Wed Thu Fri Sat
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
********** October *********
Sun Mon Tue Wed Thu Fri Sat
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
********** November *********
Sun Mon Tue Wed Thu Fri Sat
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
********** December *********
Sun Mon Tue Wed Thu Fri Sat
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.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.Calendar;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
//日曆
public class MyCalendar extends JApplet {
public static final String WEEK_SUN = “SUN”;
public static final String WEEK_MON = “MON”;
public static final String WEEK_TUE = “TUE”;
public static final String WEEK_WED = “WED”;
public static final String WEEK_THU = “THU”;
public static final String WEEK_FRI = “FRI”;
public static final String WEEK_SAT = “SAT”;
public static final Color background = Color.yellow;
public static final Color foreground = Color.black;
public static final Color headerBackground = Color.blue;
public static final Color headerForeground = Color.white;
public static final Color selectedBackground = Color.blue;
public static final Color selectedForeground = Color.white;
private JPanel cPane;
private JLabel yearsLabel;
private JSpinner yearsSpinner;
private JLabel monthsLabel;
private JComboBox monthsComboBox;
private JTable daysTable;
private AbstractTableModel daysModel;
private Calendar calendar;
int delay = 1000;
public MyCalendar() {
cPane = (JPanel) getContentPane();
}
public void init() {
setSize(350, 300);
cPane.setLayout(new BorderLayout());
calendar = Calendar.getInstance();
yearsLabel = new JLabel(“Year: “);
yearsSpinner = new JSpinner();
yearsSpinner.setEditor(new JSpinner.NumberEditor(yearsSpinner, “0000”));
yearsSpinner.setValue(new Integer(calendar.get(Calendar.YEAR)));
yearsSpinner.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent changeEvent) {
int day = calendar.get(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.YEAR, ((Integer) yearsSpinner.getValue())
.intValue());
int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar
.set(Calendar.DAY_OF_MONTH, day maxDay ? maxDay : day);
updateView();
}
});
JPanel yearMonthPanel = new JPanel();
cPane.add(yearMonthPanel, BorderLayout.NORTH);
yearMonthPanel.setLayout(new BorderLayout());
yearMonthPanel.add(new JPanel(), BorderLayout.CENTER);
JPanel yearPanel = new JPanel();
yearMonthPanel.add(yearPanel, BorderLayout.WEST);
yearPanel.setLayout(new BorderLayout());
yearPanel.add(yearsLabel, BorderLayout.WEST);
yearPanel.add(yearsSpinner, BorderLayout.CENTER);
monthsLabel = new JLabel(“Month: “);
monthsComboBox = new JComboBox();
for (int i = 1; i = 12; i++) {
monthsComboBox.addItem(new Integer(i));
}
monthsComboBox.setSelectedIndex(calendar.get(Calendar.MONTH));
monthsComboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
int day = calendar.get(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.MONTH, monthsComboBox.getSelectedIndex());
int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar
.set(Calendar.DAY_OF_MONTH, day maxDay ? maxDay : day);
updateView();
}
});
JPanel monthPanel = new JPanel();
yearMonthPanel.add(monthPanel, BorderLayout.EAST);
monthPanel.setLayout(new BorderLayout());
monthPanel.add(monthsLabel, BorderLayout.WEST);
monthPanel.add(monthsComboBox, BorderLayout.CENTER);
daysModel = new AbstractTableModel() {
public int getRowCount() {
return 9;
}
public int getColumnCount() {
return 7;
}
public Object getValueAt(int row, int column) {
if (row == 0) {
return getHeader(column);
}
row–;
Calendar calendar = (Calendar) MyCalendar.this.calendar.clone();
calendar.set(Calendar.DAY_OF_MONTH, 1);
int dayCount = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int moreDayCount = calendar.get(Calendar.DAY_OF_WEEK) – 1;
int index = row * 7 + column;
int dayIndex = index – moreDayCount + 1;
if (index moreDayCount || dayIndex dayCount) {
return null;
} else {
return new Integer(dayIndex);
}
}
};
daysTable = new CalendarTable(daysModel, calendar);
daysTable.setCellSelectionEnabled(true);
daysTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
daysTable.setDefaultRenderer(daysTable.getColumnClass(0),
new TableCellRenderer() {
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
String text = (value == null) ? “” : value.toString();
JLabel cell = new JLabel(text);
cell.setOpaque(true);
if (row == 0) {
cell.setForeground(headerForeground);
cell.setBackground(headerBackground);
} else {
if (isSelected) {
cell.setForeground(selectedForeground);
cell.setBackground(selectedBackground);
} else {
cell.setForeground(foreground);
cell.setBackground(background);
}
}
return cell;
}
});
updateView();
cPane.add(daysTable, BorderLayout.CENTER);
// 窗體添加事件監聽,監聽秒錶的觸發
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
repaint();
}
};
new Timer(delay, taskPerformer).start();
}
public void paint(Graphics g) {
int hh, mm, ss;
Calendar now;
String st;
// 獲取時間
now = Calendar.getInstance();
hh = now.get(Calendar.HOUR_OF_DAY);// 小時
mm = now.get(Calendar.MINUTE);// 分鐘
ss = now.get(Calendar.SECOND);// 秒
g.setColor(Color.WHITE);
g.fillRect(5, 250, 150, 30);// 填充的矩形
g.setColor(Color.BLACK);
if (hh 10)
st = “0” + hh;
else
st = “” + hh;
if (mm 10)
st = st + “:0” + mm;
else
st = st + “:” + mm;
if (ss 10)
st = st + “:0” + ss;
else
st = st + “:” + ss;
{
g.setFont(new Font(“華文楷體”, Font.BOLD, 16));
g.drawString(“系統時間:” + st, 10, 270);
}
}
public static String getHeader(int index) {
switch (index) {
case 0:
return WEEK_SUN;
case 1:
return WEEK_MON;
case 2:
return WEEK_TUE;
case 3:
return WEEK_WED;
case 4:
return WEEK_THU;
case 5:
return WEEK_FRI;
case 6:
return WEEK_SAT;
default:
return null;
}
}
public void updateView() {
daysModel.fireTableDataChanged();
daysTable.setRowSelectionInterval(calendar.get(Calendar.WEEK_OF_MONTH),
calendar.get(Calendar.WEEK_OF_MONTH));
daysTable.setColumnSelectionInterval(
calendar.get(Calendar.DAY_OF_WEEK) – 1, calendar
.get(Calendar.DAY_OF_WEEK) – 1);
daysTable.setColumnSelectionInterval(0,0);
}
public static class CalendarTable extends JTable {
private Calendar calendar;
public CalendarTable(TableModel model, Calendar calendar) {
super(model);
this.calendar = calendar;
}
public void changeSelection(int row, int column, boolean toggle,
boolean extend) {
super.changeSelection(row, column, toggle, extend);
if (row == 0) {
return;
}
Object obj = getValueAt(row, column);
if (obj != null) {
calendar.set(Calendar.DAY_OF_MONTH, ((Integer) obj).intValue());
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame(“簡易時間日曆”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyCalendar myCalendar = new MyCalendar();
myCalendar.init();
frame.getContentPane().add(myCalendar);
frame.setLocation(330, 80);
frame.setSize(360, 212);
frame.setVisible(true);
}
// 滾動字
public static class RollbyJFrame extends JFrame implements ActionListener,
FocusListener, javax.swing.event.ChangeListener {
private JTextField text;
private JSpinner spinner;
private Timer timer;
private JButton button;
public void focusGained(FocusEvent e) // 獲得焦點時
{
if (e.getSource() == text) {
timer.stop();
}
}
public void focusLost(FocusEvent e) // 失去焦點時
{
if (e.getSource() == text) {
timer.restart();
}
}
public void stateChanged(ChangeEvent e) {
if (e.getSource() == spinner) {
timer.setDelay(new Integer(“” + spinner.getValue())); // 設置延時的時間間隔
}
}
public void actionPerformed(ActionEvent e) // 定時器定時執行事件
{
if (e.getSource() == button)
;
else {
String temp = text.getText();
temp = temp.substring(1) + temp.substring(0, 1);
text.setText(temp);
}
}
public void buttondown(ActionEvent e) // 單擊事件
{
if (e.getSource() == button) {
}
;
}
}
}
JAVA萬年曆代碼
/*
題目:輸出任意年份任意月份的日曆表(公元後)
思路:
1.已知1年1月1日是星期日,1 % 7 = 1 對應的是星期日,2 % 7 = 2 對應的是星期一,以此類推;
2.計算當年以前所有天數+當年當月1號之前所有天數;
a.年份分平年閏年,平年365天,閏年366天;
b.閏年的判斷方法year % 400 == 0 || (year % 100 != 0 year % 4 == 0)若為真,則為閏年否則為平年;
c.定義平年/閏年數組,包含各月天數;
d.遍曆數組求和,計算當年當月前總天數;
e.當年以前所有天數+當年當月前總天數+1即為1年1月1日到當年當月1日的總天數;
3.總天數對7取模,根據結果判斷當月1號是星期幾,輸出空白區域;
4.輸出當月日曆表,逢星期六換行
*/
import java.util.Scanner;
class FindMonthList {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println(“請輸入年份:”);
int year = sc.nextInt(); //年份
if (year 1) { //判斷非法輸入年份
System.out.println(“輸入錯誤!”);
return;
}
System.out.println(“請輸入月份:”);
int month = sc.nextInt(); //月份
if (month 1 || month 12) { //判斷非法輸入月份
System.out.println(“輸入錯誤!”);
return;
}
//輸出表頭
System.out.println(“——-” + year + ” 年 ” + month + ” 月 ” + “——-“);
System.out.println();
System.out.println(“日 一 二 三 四 五 六”);
//計算當前年份以前所有天數beforeYearTotalDay;每4年一個閏年,閏年366天,平年365天
int beforeYearTotalDay = ((year – 1) / 4 * 366) + (year-1 – ((year – 1) / 4)) * 365;
int[] arrLeapYear = {0,31,29,31,30,31,30,31,31,30,31,30,31}; //閏年各月天數 int數組
int[] arrNormalYear = {0,31,28,31,30,31,30,31,31,30,31,30,31}; //平年各月天數 int數組
int beforeMonthTotalDay = 0; //定義本年當月之前月份的總天數
if (year % 400 == 0 || (year % 100 != 0 year % 4 == 0)) { //判斷當前年份是否是閏年
for (int i = 0 ; i month ; i ++ ) { //for循環計算當月之前總天數
//計算當前月份之前的所有天數
beforeMonthTotalDay = beforeMonthTotalDay + arrLeapYear[i];
}
//判斷當月1日是星期幾
int totalDay = beforeYearTotalDay + beforeMonthTotalDay + 1;
int week = totalDay % 7;//已知1年1月1日是星期日,即模7得1對應的是星期日
for (int i = 0 ; i (week – 1 + 7) % 7 ; i ++) { //如果寫成i (week-1)會出現i-1的情況
System.out.print(” “);//輸出開頭空白
}
for (int i = 1 ;i = arrLeapYear[month] ;i ++ ) { //for循環輸出各月天數
System.out.print(i + ” “);
if (i 10 ) { //小於10的數補一個空格,以便打印整齊
System.out.print(” “);
}
if (i % 7 == ((7-(week – 1)) % 7 ) || i == arrLeapYear[month]) {//每逢星期六/尾數換行
System.out.println();
}
}
} else { //不是閏年就是平年
for (int i = 0 ; i month ; i ++ ) { //for循環計算出當月之前月份總天數
beforeMonthTotalDay = beforeMonthTotalDay + arrNormalYear[i];
}
//判斷當月1日是星期幾
int totalDay = beforeYearTotalDay + beforeMonthTotalDay + 1;
int week = totalDay % 7;//已知1年1月1日是星期日,即模7得1對應的是星期日
for (int i = 0 ; i (week – 1 + 7) % 7 ; i ++) { //如果寫成i (week-1)會出現i-1的情況
System.out.print(” “);//輸出開頭空白
}
for (int i = 1 ;i = arrNormalYear[month] ;i ++ ) {//for循環輸出各月天數
System.out.print(i + ” “);
if (i 10 ) { //小於10的數補一個空格,以便打印整齊
System.out.print(” “);
}
if (i % 7 == ((7-(week – 1)) % 7 ) || i == arrNormalYear[month]) {//每逢星期六/尾數換行
System.out.println();
}
}
}
}
}
顯示效果:
java編寫萬年曆程序
希望我的回答能得到分~呵呵
/**
* @(#)AidyCalender.java
*
*
* @author
* @version 1.00 2008/7/19
*/
import java.awt.*;
import java.awt.event.*;
import java.lang.StringBuffer;
import javax.swing.*;
import java.util.*;
import javax.swing.Timer;
import javax.swing.border.*;
public class AidyCalender extends JFrame implements ActionListener,ItemListener{
Date date = new Date();
private GregorianCalendar gregorianCalendar = new GregorianCalendar();
//定義中英文字符數組存儲星期信息,用於轉換顯示
private String[] stringWeekEn = new String[] { “SUN”, “MON”, “TUE”, “WED”,
“THU”, “FRI”, “SAT” };
private String[] stringWeekCn = new String[] { “星期日”, “星期一”, “星期二”, “星期三”,
“星期四”, “星期五”, “星期六” };
//定義存儲月份的信息數組,用於轉換顯示方示
private String[] stringMonthEn = new String[] { “Jan”, “Feb”, “Mar”, “Apr”,
“May”, “Jun”, “Jul”, “Aug”, “Sept”, “Oct”, “Nov”, “Dec” };
private String[] stringMonthCn = {“一月”,”二月”,”三月”,”四月”,”五月”,”六月”,
“七月”,”八月”,”九月”,”十月”,”十一月”,”十二月”};
private String[] sysNowTime = new String[6];//sysNowTime 用於存儲系統時間的變量
private String[] sysRunTime = new String[6];
private JLabel []labelWeek = new JLabel[7];
private JLabel []labelDay = new JLabel[42];
private JLabel labelTime = new JLabel();
private JPanel panel1 = new JPanel();
private JPanel panel2 = new JPanel();
private JPanel panel3 = new JPanel();
private JComboBox combo1 = new JComboBox();
private JComboBox combo2 = new JComboBox();
private JButton buttonToday = new JButton();
private Border border = BorderFactory.createRaisedBevelBorder();
private Border border1 = BorderFactory.createLineBorder(Color.cyan,3);
public AidyCalender(String title) {
super(title);
for (int y = 1900; y 2101; y++) {
combo1.addItem(” ” + new Integer(y).toString()+”年”);
}
for (int m = 0;m12;m++){
combo2.addItem(” “+stringMonthCn[m]);
}
buttonToday.setText(“今 天”);
setLayout(new FlowLayout());
add(panel1);
add(panel2);
add(panel3);
panel1.setLayout(new GridLayout(1,3,10,0));
panel1.add(combo1);
combo1.addItemListener(this);
panel1.add(combo2);
combo2.addItemListener(this);
panel1.add(buttonToday);
buttonToday.addActionListener(this);
labelTime.setFont(new Font(“宋體”,Font.PLAIN,16));
labelTime.setForeground(Color.MAGENTA);
panel1.add(labelTime);
Timer time = new Timer(1000,new TimerListener());
time.addActionListener(new TimerListener());
//time.setRepeats(true);
time.start();
//labelTime.addAncestorListener(new TimerListener());
panel2.setLayout(new GridLayout(7,7,0,10));
panel2.setBackground(Color.white);
for(int i=0;i7;i++){
labelWeek[i] = new JLabel();
labelWeek[i].setHorizontalAlignment(0);
if(i==0||i==6){
labelWeek[i].setBackground(Color.blue);
labelWeek[i].setForeground(Color.RED);
labelWeek[i].setFont(new Font(“黑體”,Font.BOLD,14));
}
else{
labelWeek[i].setForeground(Color.BLACK);
labelWeek[i].setFont(new Font(“新宋體”,Font.PLAIN,14));
}
labelWeek[i].setText(stringWeekCn[i]);
panel2.add(labelWeek[i]);
}
for(int i= 0;i42;i++){
labelDay[i] = new JLabel();
labelDay[i].setHorizontalAlignment(0);
labelDay[i].setText(“”);
panel2.add(labelDay[i]);
}
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setSize(300,300);
setBounds(250, 200, 400, 360);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getSysDate();
setNowDate();
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==buttonToday){
setNowDate();
}
}
public void itemStateChanged(ItemEvent aa){
setChangeDate();
}
public int turnWeek(String week){
int i;
for(i=0;i7;i++)
if(week.equalsIgnoreCase(stringWeekEn[i]))
break;
return i;
}
public int turnMonth(String month){
/**
*int turnMonth(String month)
*@month 系統日期中的月,諸如Jan\Feb
*@return int
*返回一個整數值,用於尋找stringMonthCn[]數組中對應的中文月份
*/
int i;
for(i=0;i12;i++)
if(month.equalsIgnoreCase(stringMonthEn[i]))
break;
return i;
}
/**
*setNowDate()
*設置當前系統日期
*/
public void setNowDate(){
setSysDate(getNowYear(),getNowMonth());
getSysRunDate();
setDateNull();
combo1.setSelectedIndex(getShowYear() – 1900);
combo2.setSelectedIndex(getShowMonth());
setDays(getMonthDays(getNowYear(),getNowMonth()),getInitWeek(sysRunTime[0]),getNowDay());
//labelTime.setText(sysNowTime[3]);
//labelTime.setHorizontalAlignment(0);
}
/**Integer getShowYear()
*獲取組合框中應該顯示的年份
*/
public void setSysDate(int year,int month){
gregorianCalendar.set(year,month,1);
}
public void setDateNull(){
for(int i=0;i42;i++){
labelDay[i].setText(“”);
}
}
public void setChangeDate(){
setSysDate(getComboYear(),getComboMonth());
getSysRunDate();
setDateNull();
setDays(getMonthDays(getComboYear(),getComboMonth()),getInitWeek(sysRunTime[0]),-1);
}
public int getMonthDays(int year, int month) {
/**
*返回所選年月的天數,因為數組中的數值從0開始,所以3\5\8\10分別代表4\6\9\11幾個小月.
*而1代表2月,經過是否為閏年判斷,選擇返回28或29天.
*其餘月份為大月,返回31天.
**/
switch (month) {
case 3:
case 5:
case 8:
case 10:
return 30;//小月返回30天
case 1:
if (gregorianCalendar.isLeapYear(year)) {
//isLeapYear(year)確定當前紀元中的指定年份是否為閏年。
return 29;
} else {
return 28;
}//閏年的二月返回29天,平年返回28天
default:
return 31;
//大月返回31天
}
}
/**
*int getComboYear()
*獲取組合框中的年份
*/
public void getSysDate(){
date = gregorianCalendar.getTime();
sysNowTime = (date.toString()).split(” “);
}
public void getSysRunDate(){
date = gregorianCalendar.getTime();
sysRunTime = (date.toString()).split(” “);
}
public int getComboYear(){
return combo1.getSelectedIndex()+1900;
}
/**
*int getComboMonth()
*獲取月組合框中的整數值,
*/
public int getComboMonth(){
return combo2.getSelectedIndex();
}
public int getInitWeek(String initWeek){
/**
*getWeekNow(String initWeek)
*@para nowWeek 系統日期中的星期
*返回當月中的1號是從星期幾開始
*/
int nowWeek = 0 ;
for(int i = 0;i7;i++){
if(initWeek.equalsIgnoreCase(stringWeekEn[i])){
nowWeek = i;
break;
}
}
return nowWeek;
}
public int getNowYear(){
return Integer.parseInt(sysNowTime[5]);
}
public int getNowMonth(){
int nowMonth=0;
for(int i=0;i12;i++){
if(sysNowTime[1].equalsIgnoreCase(stringMonthEn[i]));
nowMonth=i;
break;
}
return nowMonth;
}
public int getNowDay(){
return Integer.parseInt(sysNowTime[2]);
}
public Integer getShowYear(){
return Integer.parseInt(sysNowTime[5]);
}
public Integer getShowMonth(){
/**
*Integer getShowMonth()
*獲取在組給框中顯示的中文格式月份:如七月\八月等
*/
return turnMonth(sysNowTime[1]);
}
public void setDays(int monthDays,int initWeek,int day){
/**
*void setDays(int monthDays,int initWeek,int day)
*@para monthDays 本月天數
*@para initWeek 初始星期
*@para day 今天日
*設置月曆
*/
setDateNull();
for(int i=initWeek;iinitWeek+monthDays+1;i++){
if((i-initWeek+1)==day){
labelDay[i].setBorder(border1);
labelDay[i].setForeground(Color.BLUE);
labelDay[i].setFont(new Font(“黑體”,Font.BOLD,20));
}else if((i%7==0)||(i%7==6))
labelDay[i].setForeground(Color.RED);
else{
labelDay[i].setForeground(Color.BLACK);
}
labelDay[i].setText(String.valueOf(i-initWeek+1));
}
for(int i=initWeek+monthDays;i42;i++)
labelDay[i].setText(“”);
}
class TimerListener implements ActionListener{
//AdapterDemo var=new AdapterDemo(“萬年曆程序–Aidy”);
public void actionPerformed(ActionEvent e) {
GregorianCalendar g = new GregorianCalendar();
String clock = new String((g.getTime().toString().split(” “))[3]);
labelTime.setText(clock);
}
}
public static void main(String args[])
{
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){
throw new RuntimeException(e);
}
AidyCalender var=new AidyCalender(“萬年曆程序–question_bai”);
}
}
java萬年曆源代碼是多少?
package org.java.test;\x0d\x0a\x0d\x0aimport java.util.Scanner;\x0d\x0apublic class CalendarTest{\x0d\x0apublic static void main(String[] args) {\x0d\x0aSystem.out.println(“歡 迎 使 用 萬 年 歷”);\x0d\x0aScanner input = new Scanner(System.in);\x0d\x0aSystem.out.print(“\n請選擇年份: “);\x0d\x0aint year = input.nextInt();\x0d\x0aSystem.out.print(“\n請選擇月份: “);\x0d\x0aint month = input.nextInt();\x0d\x0aSystem.out.println();\x0d\x0aint days = 0; // 存儲當月的天數\x0d\x0aboolean isRn;\x0d\x0a/* 判斷是否是閏年 */\x0d\x0aif (year % 4 == 0 !(year % 100 == 0) || year % 400 == 0) { // 判斷是否為閏年\x0d\x0aisRn = true; // 閏年\x0d\x0a} else {\x0d\x0aisRn = false;// 平年\x0d\x0a}\x0d\x0a/* 計算輸入的年份之前的天數 */\x0d\x0aint totalDays = 0;\x0d\x0afor (int i = 1900; i
回答於 2022-11-16
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/194377.html