如何用JAVA编写出一个简单的日历

Python018

如何用JAVA编写出一个简单的日历,第1张

import javax.swing.JOptionPane

public class NewClass{

public static void main(String[] args){

int year,month

Calender cal=new Calender(2008,10)

cal.showCalender()

year=Integer.parseInt(JOptionPane.showInputDialog("Year:"))

month=Integer.parseInt(JOptionPane.showInputDialog("Month:"))

cal.setYear(year)

cal.setMonth(month)

cal.showCalender()

}

}

class Calender{

private int year,month

public Calender(){

year=0

month=1

}

public Calender(int year){

this.year=year

month=1

}

public Calender(int year,int month){

this.year=year

if(month>12)

this.month=month%12

else

this.month=month

}

public void setYear(int year){

this.year=year

}

public void setMonth(int month){

if(month>12)

this.month=month%12

else

this.month=month

}

private int dayOfMonth(){

int days=0

switch(month){

case 1:days=31break

case 2:{

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

days=29

else

days=28

break

}

case 3:days=31break

case 4:days=30break

case 5:days=31break

case 6:days=30break

case 7:days=31break

case 8:days=31break

case 9:days=30break

case 10:days=31break

case 11:days=30break

case 12:days=31break

default:

days=0

}

return days

}

private int dayOfWeek(){

int Y=year

int M=month

int D=1

int A

A = Y>0?(5+(Y+1)+(Y-1)/4-(Y-1)/100+(Y-1)/400)%7:(5+Y+Y/4-Y/100+Y/400)%7

A = M>2?(A+2*(M+1)+3*(M+1)/5)%7:(A+2*(M+2)+3*(M+2)/5)%7

if (((Y%4 == 0 &&Y%100 != 0)|| Y%400 == 0) &&M>2) A =(A+1)%7

A=(A+D)%7

return A

}

public void showCalender(){

String str=new String()

str=" "

str+=year+"年"+month+"月"

str+="\n\n"

str+="日 一 二 三 四 五 六\n"

int week=this.dayOfWeek()

for(int i=0,j=1i<7i++){

if(i<week)

str+=" "

else{

str+=" "+j+""

j++

}

}

str+="\n"

end:

for(int i=7-week+1i<=this.dayOfMonth()){

for(int j=0j<7j++){

if(i<10)

str+=" "+i+""

else

str+=i+" "

i++

if(i>this.dayOfMonth())

break end

}

str+="\n"

}

JOptionPane.showMessageDialog(null,str)

}

}

用一个类来实现

按照你的要求编写的Java日历验证程序如下

UI.java

import java.util.Scanner

public class UI {

 static Scanner sc=new Scanner(System.in)

 public static int askInt(String s){

  System.out.print(s)

  return sc.nextInt()

 }

 public static void println(String s){

  System.out.println(s)

 }

}

EE.java

public class EE {

 public void validateDateCore(){

  int year =UI.askInt("Enter the year: ")

  int month=UI.askInt("Enter the month: ")

  int day=UI.askInt("Enter the day: ")

  if(year < 1){

   UI.println("The year is not a valid number.")

   return

  }

  if(month<1 || month>12){

   UI.println("The month is not a valid number.")

   return

  }

  int monthDay=0

  switch(month){

   case 1:

   case 3:

   case 5:

   case 7:

   case 8:

   case 10:

   case 12:monthDay=31break

   case 4:

   case 6:

   case 9:

   case 11:monthDay=30break

   case 2:

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

     monthDay=29

    }else{

     monthDay=28

    }

    break

  }

  if(day<1 || day>monthDay){

   UI.println("The day is not a valid number.")

   return

  }else{

   UI.println("It is "+day+"/"+month+"/"+year+".")

  }

 }

 public static void main(String[] args) {

  new EE().validateDateCore()

 }

}

运行结果

看看下面代码:

import java.awt.*

import java.awt.event.*

import java.util.Calendar

import javax.swing.JOptionPane

class CalendarBean

{

String day[]

int year=2011,month=0

public void setYear(int year)

{this.year=year

}

public int getYear()

{return year

}

public void setMonth(int month)

{this.month=month

}

public int getMonth()

{return month

}

public String[] getCalendar()

{ String a[]=new String[42]

Calendar 日历=Calendar.getInstance()

日历.set(year,month-1,1)

int 星期几=日历.get(Calendar.DAY_OF_WEEK)-1

int day=0

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

{ day=31

}

if(month==4||month==6||month==9||month==11)

{ day=30

}

if(month==2)

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

{ day=29

}

else

{ day=28

}

}

for(int i=星期几,n=1i<星期几+dayi++)

{

a[i]=String.valueOf(n)

n++

}

return a

}

}

class CalendarFrame extends Frame implements ActionListener

{

Label labelDay[]=new Label[42]

Button titleName[]=new Button[7]

String name[]={"日","一","二","三", "四","五","六"}

TextField text1,text2Button nextMonth,previousMonth,EnterLabel lab1,lab2,lab3

int year=2012,month=5

CalendarBean calendar

Label showMessage=new Label("",Label.CENTER)

public CalendarFrame()

{ Panel pCenter=new Panel()

pCenter.setLayout(new GridLayout(7,7))

for(int i=0i<7i++)

{ titleName[i]=new Button(name[i])

pCenter.add(titleName[i])

}

for(int i=0i<42i++)

{

labelDay[i]=new Label("",Label.CENTER)

pCenter.add(labelDay[i])

}

calendar=new CalendarBean()

calendar.setYear(year)

calendar.setMonth(month)

String day[]=calendar.getCalendar()

for(int i=0i<42i++)

{ labelDay[i].setText(day[i])

}

lab1=new Label("请输入日期")

lab2=new Label("年份")

lab3=new Label("月份")

Enter=new Button("确定")

text1=new TextField(10)

text2=new TextField(5)

nextMonth=new Button("下月")

previousMonth=new Button("上月")

Enter.addActionListener(this)

nextMonth.addActionListener(this)

previousMonth.addActionListener(this)

Panel pNorth=new Panel(),

pSouth=new Panel()

pNorth.add( lab1)

pNorth.add(lab2)

pNorth.add( text1)

pNorth.add(lab3)

pNorth.add(text2)

pNorth.add(Enter)

pNorth.add(previousMonth)

pNorth.add(nextMonth)

pSouth.add(showMessage)

showMessage.setText("日历:"+calendar.getYear()+"年"+ calendar.getMonth()+"月" )

ScrollPane scrollPane=new ScrollPane()

scrollPane.add(pCenter)

add(scrollPane,BorderLayout.CENTER)

add(pNorth ,BorderLayout.NORTH)

add(pSouth ,BorderLayout.SOUTH)

}

public void actionPerformed(ActionEvent e)

{ if(e.getSource()==nextMonth)

{ month=month+1

if(month>12)

month=1

calendar.setMonth(month)

String day[]=calendar.getCalendar()

for(int i=0i<42i++)

{ labelDay[i].setText(day[i])

}

}

else if(e.getSource()==previousMonth)

{ month=month-1

if(month<1)

month=12

calendar.setMonth(month)

String day[]=calendar.getCalendar()

for(int i=0i<42i++)

{ labelDay[i].setText(day[i])

}

}else {

String yea=text1.getText()

String mon=text2.getText()

try{

year=Integer.parseInt(yea)

month=Integer.parseInt(mon)

if(month>12||month<1||year<1){

JOptionPane.showMessageDialog(null, "请输入正确月份或月份")

return

}

else{

calendar.setYear(year)

calendar.setMonth(month)

}

String day[]=calendar.getCalendar()

for(int i=0i<42i++)

{

labelDay[i].setText(day[i])

}

}catch(NumberFormatException ee){

JOptionPane.showMessageDialog(null, "请输入正确的年份及月份")

}

}

showMessage.setText("日历:"+calendar.getYear()+"年"+calendar.getMonth()+"月" )

}

}

public class CalendarMainClass

{ public static void main(String args[])

{ CalendarFrame frame=new CalendarFrame()

frame.setTitle("日历")

frame.setBounds(300,200,500,300)

frame.setVisible(true)

frame.validate()

frame.addWindowListener(new java.awt.event.WindowAdapter()

{ public void windowClosing(java.awt.event.WindowEvent e)

{ System.exit(0)

}

}

)

}

}

有问题就追问。满意请采纳。