UI.java
import java.util.Scannerpublic 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 javax.swing.JOptionPanepublic 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)
}
}
用一个类来实现