您的位置:

java万年历,java万年历实训报告

本文目录一览:

编写 万年历java代码例如1900年一月一日是星期一。

import java.util.Calendar;

import java.util.Date;

import java.util.Scanner;

 

public class MyCalendar

{

    public static void main(String[] args)

    {

        Scanner scanner = new Scanner(System.in);

        String reg = "^(\\d+)[^\\d]+((0?[1-9])|(1[012]))$";

        while(true)

        {

            System.out.println("输入年月(年和月用非数字隔开:如2015.1)(什么都不输入直接退出)");

            String line = scanner.nextLine().trim();

            if("".equals(line))

            {

                scanner.close();

                break;

            }

            if(!line.matches(reg))

            {

                continue;

            }

            int year = Integer.parseInt(line.replaceAll(reg, "$1"));

            int month = Integer.parseInt(line.replaceAll(reg, "$2"));

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

            Calendar calendar = Calendar.getInstance();

            // 这个月的1号是星期几

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

            int day = calendar.get(Calendar.DAY_OF_WEEK);

            int start = Calendar.SUNDAY;

            calendar.add(Calendar.DATE, -day + start);

            while(start  day)

            {

                System.out.print(calendar.get(Calendar.DATE) + "\t");

                calendar.add(Calendar.DATE, 1);

                start++;

            }

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

            Date now = calendar.getTime();

            calendar.set(year, month, 1);

            Date next = calendar.getTime();

            for(Date cur = now; cur.before(next);)

            {

                calendar.setTime(cur);

                int x = calendar.get(Calendar.DATE);

                String tmp = x  10 ? "0" + x : x + "";

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

                if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)

                {

                    System.out.println();

                }

                calendar.add(Calendar.DATE, 1);

                cur = calendar.getTime();

            }

            calendar.add(Calendar.DATE, -1);

            int to = calendar.get(Calendar.DAY_OF_WEEK);

            int end = Calendar.SATURDAY;

            while(to  end)

            {

                calendar.add(Calendar.DATE, 1);

                int x = calendar.get(Calendar.DATE);

                String tmp = x  10 ? "0" + x : x + "";

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

                to++;

            }

            System.out.println();

        }

    }

}

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;icomponentCount;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 swing带界面的万年历代码

按照你的要求编写的Java swing 带界面的万年历代码如下

//日历

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Calendar;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

public class CCI extends JFrame implements ActionListener{

 JButton jb1=new JButton("");

 JButton jb2=new JButton("");

 JButton jb3=new JButton("");

 JButton jb4=new JButton("");

 JPanel jp1=new JPanel();

 JPanel jp2=new JPanel();

 JPanel jp3=new JPanel();

 JPanel jp4=new JPanel();

 JLabel jl1=new JLabel();

 JLabel jl2=new JLabel();

 JLabel[]jl=new JLabel[49];

 String []week={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};

 Calendar c=Calendar.getInstance();

 int year,month,day;

 int nowyear,nowmonth,nowday;

 CCI(){

  super("简单日历");

  nowyear=c.get(Calendar.YEAR);

  nowmonth=c.get(Calendar.MONTH)+1;

  nowday=c.get(Calendar.DAY_OF_MONTH);

  year=nowyear;

  month=nowmonth;

  day=nowday;

  String s=year+"年"+month+"月";

  jl1.setForeground(Color.RED);

  jl1.setFont(new Font(null,Font.BOLD,20));

  jl1.setText(s);

  jb1.addActionListener(this);

  jb2.addActionListener(this);

  jb3.addActionListener(this);

  jb4.addActionListener(this);

  jp1.add(jb1);jp1.add(jb2);jp1.add(jl1);jp1.add(jb3);jp1.add(jb4);

  jp2.setLayout(null);

  createMonthPanel();

  jp2.add(jp3);

  jl2.setFont(new Font(null,Font.BOLD,20));

  jl2.setText("今天是"+nowyear+"年"+nowmonth+"月"+nowday+"日");

  jp4.add(jl2);

  add(jp1,BorderLayout.NORTH);

  add(jp2,BorderLayout.CENTER);

  add(jp4,BorderLayout.SOUTH);

  setSize(500,500);

  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  setLocationRelativeTo(null);

  setVisible(true);

 }

 @Override

 public void actionPerformed(ActionEvent ae) {

  if(ae.getSource()==jb1){

   year=year-1;

   String s=year+"年"+month+"月";

   jl1.setText(s);

   jp3.removeAll();

   createMonthPanel();

   jp3.validate();

  }

  if(ae.getSource()==jb2){

   if(month==1){

    year=year-1;

    month=12;

   }else{

    month=month-1;

   }

   String s=year+"年"+month+"月";

   jl1.setText(s);

   jp3.removeAll();

   createMonthPanel();

   jp3.validate();

  }

  if(ae.getSource()==jb3){

   if(month==12){

    year=year+1;

    month=1;

   }else{

    month=month+1;

   }

   String s=year+"年"+month+"月";

   jl1.setText(s);

   jp3.removeAll();

   createMonthPanel();

   jp3.validate();

  }

  if(ae.getSource()==jb4){

   year=year+1;

   String s=year+"年"+month+"月";

   jl1.setText(s);

   jp3.removeAll();

   createMonthPanel();

   jp3.validate();

  }

 }

 public static void main(String[] args) {

  new CCI();

 }

 public int getMonthDays(int year, int month) { 

  switch (month) {

   case 1: 

   case 3: 

   case 5: 

   case 7:

   case 8: 

   case 10: 

   case 12:

    return 31; 

   case 2: 

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

     return 29; 

    } else { 

     return 28; 

    } 

   default: 

    return 30; 

  } 

 } 

 public void createMonthPanel(){

  c.set(year, month-1, getMonthDays(year,month));

  int weekOfMonth=c.get(Calendar.WEEK_OF_MONTH);

  if(weekOfMonth==6){

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

   jp3.setBounds(50, 20, 420, 350);

  }else{

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

   jp3.setBounds(50, 20, 420, 300);

  }

  jp3.setBorder(BorderFactory.createEtchedBorder());

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

   jl[i]=new JLabel(week[i],JLabel.CENTER);

   jl[i].setFont(new Font(null,Font.BOLD,20));

   jl[i].setBorder(BorderFactory.createEtchedBorder());

   jp3.add(jl[i]);

  }

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

  int emptyFirst=c.get(Calendar.DAY_OF_WEEK)-1;

  int daysOfMonth=getMonthDays(year,month);

  for(int i=6+emptyFirst;i=7;i--){

   int intyear=year;

   int intmonth=month;

   if(intmonth==1){

    intyear=intyear-1;

    intmonth=12;

   }else{

    intmonth=intmonth-1;

   }

   int intdays=getMonthDays(intyear,intmonth);

   jl[i]=new JLabel((intdays+7-i)+"",JLabel.CENTER);

   jl[i].setFont(new Font(null,Font.BOLD,20));

   jl[i].setForeground(Color.GRAY);

   jl[i].setBorder(BorderFactory.createEtchedBorder());

   jp3.add(jl[i]);

  }

  for(int i=7+emptyFirst;idaysOfMonth+7+emptyFirst;i++){

   jl[i]=new JLabel((i-7-emptyFirst+1)+"",JLabel.CENTER);

   jl[i].setFont(new Font(null,Font.BOLD,20));

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

    jl[i].setForeground(Color.RED);

   }else if((i-7-emptyFirst+1)==nowdaymonth==nowmonthyear==nowyear)

    jl[i].setForeground(Color.BLUE);

   else

    jl[i].setForeground(Color.BLACK);

   

   jl[i].setBorder(BorderFactory.createEtchedBorder());

   jp3.add(jl[i]);

  }

  if(weekOfMonth==6)

   for(int i=48;i=daysOfMonth+emptyFirst+7;i--){

    jl[i]=new JLabel((49-i)+"",JLabel.CENTER);

    jl[i].setFont(new Font(null,Font.BOLD,20));

    jl[i].setForeground(Color.GRAY);

    jl[i].setBorder(BorderFactory.createEtchedBorder());

    jp3.add(jl[i]);

   }

  else

   for(int i=41;i=daysOfMonth+emptyFirst+7;i--){

    jl[i]=new JLabel((42-i)+"",JLabel.CENTER);

    jl[i].setFont(new Font(null,Font.BOLD,20));

    jl[i].setForeground(Color.GRAY);

    jl[i].setBorder(BorderFactory.createEtchedBorder());

    jp3.add(jl[i]);

   }

 }

}

java万年历源代码

直接输入你先要的就可以了 当然了现在一般都不用这么麻烦的 现在有全面的一个简写版本可以直接用 谢谢

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

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