my date 2
/**
* @(#)MyDate2.java
*
*
* @author
* @version 1.00 2009/10/28
*/
import javax.swing.JOptionPane;
import java.util.Calendar;
public class MyDate2 {
private int ngay; // 1-31
private int thang; // 1-12
private int nam;
/* phuong thuc set*/
public void setngay(int ng){
this.ngay=ng;
}
public void setthang(int th){
this.thang=th;
}
public void setnam(int n){
this.nam=n;
}
/* phuong thuc get*/
public int getngay(){
return ngay;
}
public int getthang(){
return thang;
}
public int getnam(){
return nam;
}
/* phuong thuc khoi tao mac dinh*/
public MyDate2(){
Calendar cal=Calendar.getInstance();//ngay thang nam hien tai
setngay(cal.get(Calendar.DAY_OF_MONTH));
setthang(cal.get(Calendar.MONTH)+1);
setnam(cal.get(Calendar.YEAR));
}
/* phuong thuc khoi tao 3 tham so*/
public MyDate2(int d,int m,int y){
setngay(d);
setthang(m);
setnam(y);
}
/* phuong thuc khoi tao 1tham so co kieu la doi tuong */
public MyDate2(MyDate2 x){
this.ngay=x.ngay;
this.thang=x.thang;
this.nam=x.nam;
}
/* nhap*/
public void nhap(){
int ingay,ithang,inam;
do{
ithang=Integer.parseInt(JOptionPane.showInputDialog(null,"nhap thang:"));
}while(ithang<1||ithang>12);
setthang(ithang);
do{
inam=Integer.parseInt(JOptionPane.showInputDialog(null,"nhap nam:"));
}while(inam<=0);
switch(ithang){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:do{
ingay=Integer.parseInt(JOptionPane.showInputDialog(null,"nhap ngay:"));
}while(ingay<1||ingay>31);
setngay(ingay);
break;
case 4:
case 6:
case 9:
case 11: do{
ingay=Integer.parseInt(JOptionPane.showInputDialog(null,"nhap ngay:"));
}while(ingay<1||ingay>30);
setngay(ingay);
break;
case 2:if((inam%4==0&& inam%100!=0)||(inam%400==0)){
do{
ingay=Integer.parseInt(JOptionPane.showInputDialog(null,"nhap ngay:"));
}while(ingay<1||ingay>29);
setngay(ingay);
break;
}
else{
do{
ingay=Integer.parseInt(JOptionPane.showInputDialog(null,"nhap ngay:"));
}while(ingay<1||ingay>28);
setngay(ingay);
break;
}
}
}
/* hien thi*/
public void show(){
Calendar cal=Calendar.getInstance();
cal.set(getnam(),getthang()-1,getngay());
JOptionPane.showMessageDialog(null,
((cal.get(Calendar.DAY_OF_WEEK)==1)?"Chu nhat":"Thu " +
cal.get(Calendar.DAY_OF_WEEK))+ ", ngay " + getngay() + "/"+ getthang() + "/" + getnam());
}
/* main*/
public static void main(String args[]){
/*ham khoi tao mac dinh*/
MyDate2 ex1=new MyDate2();
ex1.show();
/* ham ktao 3 tham so*/
MyDate2 ex2=new MyDate2(29,10,2009);
ex2.show();
/* ham ktao 1 tham so*/
MyDate2 ex3=new MyDate2(30,10,2009);
MyDate2 ex4=new MyDate2(ex3);
ex4.ngay=30;
ex4.thang=10;
ex4.nam=2009;
ex4.show();
MyDate2 ex5=new MyDate2();
ex5.nhap();
ex5.show();
}
}
Bạn đang đọc truyện trên: Truyen4U.Com