您的位置:

java万年历设置假期,java制作万年历

本文目录一览:

java 实现万年历

首先你要明白1900年一月一日是星期一

import java.util.*;

public class WanNianLi{

public static void main(String args[]){

Scanner input=new Scanner(System.in);

System.out.print("请输入年份:");

int year=input.nextInt();

System.out.print("请输入月份:");

int month=input.nextInt();

int totalDays=1,monthDays=0;

//计算总天数

for(int i=1900;iyear;i++){

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

totalDays=totalDays+366;

}else{

totalDays=totalDays+365;

}

}

for(int passedMonth=1;passedMonth=month;passedMonth++){

if(passedMonth==1||passedMonth==3||passedMonth==5||passedMonth==7||passedMonth==8||passedMonth==10||passedMonth==12){

monthDays=31;

}else if(passedMonth==2){

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

monthDays=29;

}else{

monthDays=28;

}

}else{

monthDays=30;

}

if(passedMonthmonth){

totalDays=totalDays+monthDays;

}

}////计算总天数结束

//打印格式,对齐

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

int temp = (totalDays%7);

for (int p=0; ptemp;p++) {

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

}

for (int w=1; w=monthDays;w++) {

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

if ((totalDays+w)%7==0) {

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

}

}

}

}

java控制台万年历

JDK提供的日期类已经够用了。给你贴一个程序,你把年份,月份,日期(你可以固定为1)输入到开始的三个参数就可以了。

import java.util.*;

class TestDemo

{

public static void main(String[] args)

{

int year = 2012;

int month = 7;

int day = 16;

Calendar calendar = Calendar.getInstance();//获得一个日历

calendar.set(year, month-1, day);//设置当前时间,月份是从0月开始计算

int number = calendar.get(Calendar.DAY_OF_WEEK);//星期表示1-7,是从星期日开始,

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

System.out.println(str[number]);

}

}

用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("万年历程序--Aidy");

}

}

本文来自CSDN博客,转载请标明出处:

如何用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; irowsCount; i++){

int j = 0;

if(i==0){

j = min_week-1;

for(int o=0; omin_week-1; o++){

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

}

}

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

}

}

这个修改一下应该可以!!!