BUT2/DEV/DEV2.1/TP04_Classes_et_objets/Date.java~

52 lines
1.2 KiB
Java
Raw Normal View History

2023-10-23 13:23:36 +02:00
public class Date{
private int annee;
private int mois;
private int jour;
private static int[] jourDuMois = {31,28,31,30,31,30,31,31,30,31,30,31};
public Date(int annee, int mois, int jour){
this.annee = annee;
this.mois = mois;
this.jour = jour;
}
public String toString(){
String stringAnnee = add0(this.annee, 4);
String stringMois = add0(this.mois, 2);
String stringJour = add0(this.jour, 2);
return stringAnnee + "-" + stringMois + "-" + stringJour;
}
public void lendemain(){
this.jour ++;
if (this.jour > Date.jourDuMois[this.mois-1]){
this.jour = 1;
this.mois ++;
if (this.mois > 12){
this.mois = 1;
this.annee ++;
}
}
}
public boolean compare(Date date){
if (this.annee == date.annee && this.mois == date.mois && this.jour == date.jour){
return true;
}
return false;
}
public static String add0(int nombre, int taille){
int puissance10 = 1;
String resultat = Integer.toString(nombre);
for (int i=1; i<taille ; i++){
puissance10 *= 10;
}
while (nombre<puissance10){
resultat = "0" + resultat;
nombre *= 10;
}
return resultat;
}
}