×

java万年历 java

java万年历(用java语言编写万年历)

admin admin 发表于2022-09-02 05:06:02 浏览96 评论0

抢沙发发表评论

本文目录

用java语言编写万年历


给你一个现成的,我自己写的。
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
public class WanNianLi extends JFrame implements ActionListener {
private static int year,month,days;
private JButton btn=new JButton[days];

WanNianLi() {
super(“万年历“);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout bl=new GridLayout(5,7);
JPanel pane=new JPanel();
pane.setLayout(bl);

for (int i=0;i《days;i++) {
int temp=i+1;
btn[i]=new JButton(““+temp);
btn[i].addActionListener(this);
pane.add(btn[i]);
}
setContentPane(pane);
pack();
setLookAndFeel();
setVisible(true);

}

public static void main(String args) {
if (args.length》0)
year=Integer.parseInt(args);
else
year=1982;
if (args.length》1)
month=Integer.parseInt(args);
else
month=1;
GetDays gd=new GetDays(year,month);
days=gd.getDays();
new WanNianLi();
}

public void actionPerformed(ActionEvent evt) {
Object src=evt.getSource();
for (int i=0;i《days;i++)
if (src==btn[i]) {
int day=i+1;
GetWeekday gw=new GetWeekday(year,month,day);
String str=““;
switch (gw.getWeekday()) {
case 1:
str=“天“;
break;
case 2:
str=“一“;
break;
case 3:
str=“二“;
break;
case 4:
str=“三“;
break;
case 5:
str=“四“;
break;
case 6:
str=“五“;
break;
case 7:
str=“六“;
break;
}
setTitle(year+“年“+month+“月“+day+“日“+“星期“+str);
repaint();
}
}

private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);
}catch(Exception e){
System.out.print(e.toString());
}
}

}
//////////////
//获取星期几//
//////////////
class GetWeekday {
private Calendar cal=Calendar.getInstance();
private static int weekday;

public int getWeekday() {
return weekday;
}

GetWeekday(int y,int m,int d) {
cal.clear();
cal.set(Calendar.YEAR,y);
cal.set(Calendar.MONTH,m-1);
cal.set(Calendar.DAY_OF_MONTH,d);
weekday=cal.get(Calendar.DAY_OF_WEEK);
}
}
////////////////////
//获取当前月的天数//
////////////////////
class GetDays {
private static int days;

public int getDays() {
return days;
}

GetDays(int y,int m) {
GregorianCalendar gc=new GregorianCalendar();

switch (m) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days=31;
break;
case 4:
case 6:
case 9:
case 11:
days=30;
break;
case 2:
if (gc.isLeapYear(y))
days=29;
else
days=28;
break;
}
}
}

java万年历程序如何编写


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
*/
我自己弄的,你参考下

java 如何用calendar类生成万年历


以下是两个类,请楼主分别存成两个java文件:
其中
MainFrame.java是显示日历程序,Clock.java是日历计算程序。编译后运行MainFrame这个类即可。
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;
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(); // 实例化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);// 将显示的字符设置为居中
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()); // 得到当前所选年份
c_month = Integer.parseInt(com2.getSelectedItem().toString()) - 1; // 得到当前月份,并减1,计算机中的月为0-11
c_week = use(c_year, c_month); // 调用函数use,得到星期几
Resetday(c_week, c_year, c_month); // 调用函数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); // 将星期日的日期设置为红色
}
for (int i = 6; i 《 49; i = i + 7) {
label[i].setForeground(Color.green);// 将星期六的日期设置为绿色
}
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)); // 得到当前时间的月份
year = (int) (now.get(Calendar.YEAR)); // 得到当前时间的年份
com1.setSelectedIndex(year - 1); // 设置下拉列表显示为当前年
com2.setSelectedIndex(month_num); // 设置下拉列表显示为当前月
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); // 设置时间为所要查询的年月的第一天
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; // 存储月份的天数
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); // 前个月
month_day_score = cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 最后一天
for (int i = 7; i 《 49; i++) { // 初始化标签
label[i].setText(““);
}
week_log = week_log + 6; // 将星期数加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;
//显示时间的类:Clock
/** *//**
* Clock.java
* Summary 数字时间显示
* 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); //实例化线程
t.start(); //调用线程
}
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“);//格式化时间显示类型
Calendar now=Calendar.getInstance();
time=SDF.format(now.getTime()); //得到当前日期和时间
g.setFont(f);
g.setColor(Color.orange);
g.drawString(time,45,25);
}
}
-JAVA

如何用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

用JAVA编写一个万年历


import java.io.*;
class putout{
public void putout(int f,int x,int y){
int i;
int a= new int;
System.out.println(“ 日 一 二 三 四 五 六 “+“ “+f+“月“);
for (i=0;i《x;i++)
{System.out.print(“ “); }
for(i=x;i《x+y;i++)
a[i]=i-x+1;
for(i=x;i《x+y;i++)
{
if ((i%7==0)&&(i》0))
System.out.print(“\n“);
if (a[i]《10)
System.out.print(“ “+a[i]);
else System.out.print(“ “+a[i]);
}
System.out.println(“\n“);
}
}

class st{
public static void main(String args)throws IOException{
putout p=new putout();
int year,mouth,y=1,t,i;
InputStreamReader ir;
BufferedReader in;
ir=new InputStreamReader(System.in);
in=new BufferedReader(ir);
System.out.print(“请输入一个年份:“);
String s=in.readLine();
year=Integer.parseInt(s);
if((year%4==0 && year%100!=0)||(year%400==0))
mouth=1;
else
mouth=0;
y=year;
for(i=1;i《year;i++)
{if((i%4==0 && i%100!=0)||(i%400==0))
y++;}
y=y%7;
for(i=1;i《13;i++){
switch(i){
case 1: {p.putout(1,y,31);y=(y+31)%7;break;}
case 2: {p.putout(2,y,28+mouth);y=(y+28+mouth)%7;break;}
case 3: {p.putout(3,y,31);y=(y+31)%7;break;}
case 4: {p.putout(4,y,30);y=(y+30)%7;break;}
case 5: {p.putout(5,y,31);y=(y+31)%7;break;}
case 6: {p.putout(6,y,30);y=(y+30)%7;break;}
case 7: {p.putout(7,y,31);y=(y+31)%7;break;}
case 8: {p.putout(8,y,31);y=(y+31)%7;break;}
case 9: {p.putout(9,y,30);y=(y+30)%7;break;}
case 10: {p.putout(10,y,31);y=(y+31)%7;break;}
case 11: {p.putout(11,y,30);y=(y+30)%7;break;}
case 12: {p.putout(12,y,31);y=(y+31)%7;break;}
}
}
}
}
-JAVA

如何用java做万年历


import java.util.Calendar;
public class DateBean
{
private Calendar calendar = Calendar.getInstance();
public DateBean(int year, int month, int date){
calendar.set(year,month-1,date);
}

/*
*获取月份中的第一个的星期数
*1-7星期日到星期六
*@return int
*/
public int getFistWeek(){
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);

calendar.set(year,month,1);
return calendar.get(Calendar.DAY_OF_WEEK);
}

/*
*当月最大天数
*@return int
*/
public int getMaxDate(){
int maxDate = calendar.getActualMaximum(Calendar.DATE);
return maxDate;
}

public void showCalendar(){
System.out.println(“日\t一\t二\t三\t四\t五\t六“);
int min_week = getFistWeek();
int maxDate = getMaxDate();
int tempDate = 0;
//共有几周
int rowsCount = (maxDate-(7-min_week))/7;
if((maxDate-(7-min_week))%7 == 0){
rowsCount += 1;
}else{
rowsCount += 2;
}

for(int i=0; i《rowsCount; i++){
int j = 0;
if(i==0){
j = min_week-1;
for(int o=0; o《min_week-1; o++){
System.out.print(“ \t“);
}
}
for(; j《7; j++){
if(tempDate》=maxDate){
break;
}
tempDate++;
System.out.print(tempDate+“\t“);
}
System.out.println();
}
}

public static void main(String args){
DateBean dateBean = new DateBean(2010,11,19);
dateBean.showCalendar();
}
}
这个修改一下应该可以!!!
-java

如何用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

用JAVA实现万年历功能


import java.text.SimpleDateFormat;
import java.util.Calendar;
public class TestDate {
public static final String weeks = { “日“, “一“, “二“, “三“, “四“, “五“, “六“ };

public static void main(String args) {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR,2011);//2011年
c.set(Calendar.MONTH,0);//java中Calendar类,月从0开始, 0代表一月
c.set(Calendar.DATE,1);//1号
int day = c.get(Calendar.DAY_OF_WEEK);//获致是本周的第几天地, 1代表星期天...7代表星期六
System.out.println(new SimpleDateFormat( “yyyy-MM-dd “).format(c.getTime()));
System.out.println(“星期“ + weeks[day-1]);
}
}
把以上测试代码写作一个方法 方法的参数名为年月日, 即可。当然Calendar 还有很多功能,比如一周的第几天,一年的第几个月……
-java

JAVA编写一个多功能万年历程序


import java.text.SimpleDateFormat; import java.util.Calendar; public class TestDate { public static final String weeks = { “日“, “一“, “二“, “三“, “四“, “五“, “六“ }; public static void main(String args) { Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR,2011);//2011年 c.set(Calendar.MONTH,0);//java中Calendar类,月从0开始, 0代表一月 c.set(Calendar.DATE,1);//1号 int day = c.get(Calendar.DAY_OF_WEEK);//获致是本周的第几天地, 1代表星期天...7代表星期六 System.out.println(new SimpleDateFormat( “yyyy-MM-dd “).format(c.getTime())); System.out.println(“星期“ + weeks[day-1]); } } 把以上测试代码写作一个方法 方法的参数名为年月日, 即可。当然Calendar 还有很多功能,比如一周的第几天,一年的第几个月……
-JAVA