java万年历程序如何编写?

 我来答
别致且昂扬灬饼子3985
2011-05-25 · 超过14用户采纳过TA的回答
知道答主
回答量:65
采纳率:0%
帮助的人:43.6万
展开全部
package calendar;

import java.applet.Applet;
import java.awt.*;
import java.util.*;

public class CalendarApplet extends Applet{
private static final long serialVersionUID = 1L;
static final int TOP = 70; //顶端距离
static final int CELLWIDTH=50,CELLHEIGHT = 30; //单元格尺寸
static final int MARGIN = 3; //边界距离
static final int FEBRUARY = 1;

TextField tfYear = new TextField("2004", 5); //显示年份的文本域
Choice monthChoice = new Choice(); //月份选择下拉框
Button btUpdate = new Button("更新"); //更新按钮
GregorianCalendar calendar=new GregorianCalendar(); //日历对象
Font smallFont = new Font("TimesRoman", Font.PLAIN, 15); //显示小字体
Font bigFont = new Font("TimesRoman", Font.BOLD, 50); //显示大字体
String days[] = {"星期日", "星期一", "星期二", "星期三","星期四", "星期五", "星期六"};
String months[] = {"一月", "二月", "三月", "四月","五月", "六月", "七月", "八月", "九月","十月", "十一月", "十二月"};
int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //每个月的天数
int searchMonth,searchYear; //查询的年份及月份

public void init(){
setBackground(Color.white); //设置背景颜色
searchMonth = calendar.get(Calendar.MONTH); //得到系统年份
searchYear = calendar.get(Calendar.YEAR); //得到系统月份
add(new Label(" 年:")); //增加组件到Applet
tfYear.setText(String.valueOf(searchYear)); //设置文本域文字
add(tfYear);
add(new Label(" 月:"));
setSize(360,230);
monthChoice.setFont(smallFont); //设置月份选择下拉框的显示字体
for (int i = 0; i < 12; i++) {
monthChoice.add(months[i]); //增加下拉框选项
}
monthChoice.select(searchMonth); //设置下拉框当前选择项
add(monthChoice);
add(btUpdate);
int componentCount=this.getComponentCount(); //得到Applet中的组件数量
for (int i=0;i<componentCount;i++){
getComponent(i).setFont(smallFont); //设置所有组件的显示字体
}
}

public void paint(Graphics g){
FontMetrics fontMetric; //显示字体的FontMetrics对象
int fontAscent;
int dayPos;
int totalWidth, totalHeight; //总的宽度,高度
int numRows; //行数
int xNum, yNum; //水平和垂直方向单元格数量
int numDays;
String dayStr; //显示天数字符串

g.setColor(Color.lightGray); //设置当前颜色
g.setFont(bigFont); //设置当前使用字体
g.drawString(searchYear+"年",60,TOP+70); //绘制字符串
g.drawString((searchMonth+1)+"月",200,TOP+130);

g.setColor(Color.black);
g.setFont(smallFont);
fontMetric = g.getFontMetrics(); //获取变量初值
fontAscent = fontMetric.getAscent();
dayPos = TOP + fontAscent / 2;
totalWidth = 7 * CELLWIDTH; //得到总的表格宽度
for (int i = 0; i < 7; i++) {
g.drawString(days[i], (CELLWIDTH-fontMetric.stringWidth(days[i]))/2 + i*CELLWIDTH,dayPos-20); //绘制表格标题栏
}
numRows = getNumberRows(searchYear, searchMonth); //计算需要的行的数量
totalHeight = numRows * CELLHEIGHT; //得到总的表格高度
for (int i = 0; i <= totalWidth; i += CELLWIDTH) {
g.drawLine(i, TOP , i, TOP+ totalHeight); //绘制表格线
}
for (int i = 0, j = TOP ; i <= numRows; i++, j += CELLHEIGHT) {
g.drawLine(0, j, totalWidth, j); //绘制表格线
}
xNum = (getFirstDayOfMonth(searchYear, searchMonth) + 1) * CELLWIDTH - MARGIN;
yNum = TOP + MARGIN + fontAscent;
numDays = daysInMonth[searchMonth] + ((calendar.isLeapYear(searchYear) && (searchMonth == FEBRUARY)) ? 1 : 0);
for (int day = 1; day <= numDays; day++) {
dayStr = Integer.toString(day);
g.drawString(dayStr, xNum - fontMetric.stringWidth(dayStr), yNum); //绘制字符串
xNum += CELLWIDTH;
if (xNum > totalWidth) {
xNum = CELLWIDTH - MARGIN;
yNum += CELLHEIGHT;
}
}
}

public boolean action(Event e, Object o){
int searchYearInt;
if (e.target==btUpdate){
searchMonth = monthChoice.getSelectedIndex(); //得到查询月份
searchYearInt = Integer.parseInt(tfYear.getText(), 10); //得到查询年份
if (searchYearInt > 1581) {
searchYear = searchYearInt;
}
repaint(); //重绘屏幕
return true;
}
return false;
}

private int getNumberRows(int year, int month) { //得到行数量
int firstDay;
int numCells;
if (year < 1582) { //年份小于1582年,则返回-1
return (-1);
}
if ((month < 0) || (month > 11)) {
return (-1);
}
firstDay = getFirstDayOfMonth(year, month); //计算月份的第一天

if ((month == FEBRUARY) && (firstDay == 0) && !calendar.isLeapYear(year)) {
return 4;
}
numCells = firstDay + daysInMonth[month];
if ((month == FEBRUARY) && (calendar.isLeapYear(year))) {
numCells++;
}
return ((numCells <= 35) ? 5 : 6); //返回行数
}

private int getFirstDayOfMonth(int year, int month) { //得到每月的第一天
int firstDay;
int i;
if (year < 1582) { //年份小于1582年,返回-1
return (-1);
}
if ((month < 0) || (month > 11)) { //月份数错误,返回-1
return (-1);
}
firstDay = getFirstDayOfYear(year); //得到每年的第一天
for (i = 0; i < month; i++) {
firstDay += daysInMonth[i]; //计算每月的第一天
}
if ((month > FEBRUARY) && calendar.isLeapYear(year)) {
firstDay++;
}
return (firstDay % 7);
}

private int getFirstDayOfYear(int year){ //计算每年的第一天
int leapYears;
int hundreds;
int fourHundreds;
int first;
if (year < 1582) { //如果年份小于1582年
return (-1); //返回-1
}
leapYears = (year - 1581) / 4;
hundreds = (year - 1501) / 100;
leapYears -= hundreds;
fourHundreds = (year - 1201) / 400;
leapYears += fourHundreds;
first=5 + (year - 1582) + leapYears % 7; //得到每年第一天
return first;
}
}
/*
* DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
* dateFormat.format(new Date());//1987-02-17 21:02:01
*/
我自己弄的,你参考下
papbvhj
2011-05-26 · TA获得超过722个赞
知道答主
回答量:487
采纳率:100%
帮助的人:0
展开全部
/**
* @(#)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;m<12;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;i<7;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;i<42;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;i<7;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;i<12;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;i<42;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;i<7;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;i<12;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;i<initWeek+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;i<42;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("万年历程序--Aidy");

}

}

本文来自CSDN博客,转载请标明出处:
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
QQ1259225899
2011-05-25 · 超过14用户采纳过TA的回答
知道答主
回答量:39
采纳率:0%
帮助的人:26.2万
展开全部
我给你一份源代码: package com.accp;

import java.util.Scanner;

/**
* 日历类的方法
* @author 王毅
* 2010/10/18号制作
*/
public class CalenderMethodWy {

//以下是公共变量
/**
* 存储用户输入的月份的天数
*/
private int days=0;

/**
* 接收用户输入的年份
*/
private int year;

/**
* 接收用户输入的月份
*/
private int month;

/**
* 存储从1900/1/1到用户输入的年份和月份(之前)的总经历天数
*/
private int totalDays=0;

/**
* 计算到底这一年是否是闰年
*/
private boolean isRn;

private Scanner input=new Scanner(System.in);

/**
* 显示欢迎信息
*/
private void Welcome(){
System.out.println("******************欢迎使用万年历***************\n");
System.out.println("请输入年份:");
year=input.nextInt();
System.out.println("请输入月份:");
month=input.nextInt();
}

/**
* 计算是否是闰年
* @param passYear
* @return
*/
private boolean isRunNian(int passYear){
if(passYear % 4==0 && passYear % 100!=0 || passYear % 400==0){
isRn=true;
}else{
isRn=false;
}
return isRn;
}

/**
* 计算2月份有多少天
* @param passYear
* @return
*/
private int getYearOfDay(int passYear){
if(passYear % 4==0 && passYear %100!=0 || passYear %400==0){
return 29;
}else{
return 28;
}
}

/**
* 计算从1900/1/1到用户输入的年份之前的所有天数
*/
private void GetTotalDays(){
for(int i=1900;i<year;i++){
if(isRunNian(i)){
totalDays=totalDays+366;
}else{
totalDays=totalDays+365;
}
}
}

/**
* 计算用户输入的年从1月份到输入月份之前所经历的天数
*/
private void setBeforeDays(){
int beforeDays=0;
for(int i=1;i<=month;i++){
switch(i){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days=31;
break;
case 2:
days=getYearOfDay(year);
break;
default:
days=30;
}

if(i<month){
beforeDays=beforeDays+days;
}
}

totalDays=totalDays+beforeDays;
}

/**
* 显示(打印)日历
*/
private void printCalender(){
//存储当月第一天是星期几:星期日为0,星期一~星期六分别为1~6
int firstDayOfMonth;

//星期几=1+总天数 %7;
int temp=1+totalDays%7;

if(temp==7){ //周日
firstDayOfMonth=0;
}else{
firstDayOfMonth=temp;
}
System.out.println(year+"年"+month+"月的1号是星期"+firstDayOfMonth);

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

//输出制作位(\t)
for(int i=0;i<firstDayOfMonth;i++){
System.out.print("\t");
}

//循环输出该用的天数
for(int j=1;j<=days;j++){
System.out.print(j+"\t");

//在循环里面计算到底哪一天是星期六:
//公式:(总天数+j(外层循环变量)-1)%7==5 这个就是计算星期六
if( (totalDays+j-1)%7==5){
System.out.println();
}

}
}

/**
* 这个方法是用户调用的,其它的方法用户不能看到
*/
public void DisplayCalender(){
Welcome();
GetTotalDays();
setBeforeDays();
printCalender();
}

}

这个是测试类:
package com.accp;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {

CalenderMethodWy wy=new CalenderMethodWy();
wy.DisplayCalender();

}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
maigelin
2011-05-25
知道答主
回答量:12
采纳率:0%
帮助的人:2.1万
展开全部
逻辑判断:switch..case
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
hoeng119
2011-05-25 · TA获得超过127个赞
知道小有建树答主
回答量:158
采纳率:0%
帮助的人:93.2万
展开全部
java 不是很清楚,asp的要不?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(4)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式