Advertisement
progra1

Martes 18/9 Fecha

Sep 18th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.65 KB | None | 0 0
  1. package martes18;
  2.  
  3. public class Fecha {
  4.     //VARIABLES DE INSTANCIA
  5.     private int dia;
  6.     private int mes;
  7.     private int anio;
  8.     // se ponen en private las variables de instancia para que no puedan ser modificadas ni leidas. Para mostrarlas se necesita crear los get
  9.     //CONSTRUCTORES
  10.     Fecha(){
  11.         this.dia=32;
  12.         this.mes=1;
  13.         this.anio=2000;
  14.     }
  15.     Fecha(int d,int m, int a){
  16.         this.dia=d;
  17.         this.mes=m;
  18.         this.anio=a;
  19.     }
  20.     //METODOS
  21.     void mostrar(){
  22.         System.out.println(this.dia+"/"+this.mes+"/"+this.anio);
  23.     }
  24.     static boolean esBisiesto(int anio){
  25.         if((anio%4==0&&anio%100!=0)||anio%400==0)
  26.             return true;
  27.         else
  28.             return false;
  29.     }
  30.     //en los metodos static(metodosdeclase) no se puede usar el this.
  31.     static int diasDelMes(int mes,int anio){
  32.         //cuantos dia tiene el mes
  33.         if (mes==4||mes==6||mes==9||mes==11){
  34.             return 30;
  35.         }
  36.         if (mes==2){
  37.             if (esBisiesto(anio)){
  38.             return 29;
  39.             }
  40.             else
  41.                 return 28;
  42.         }
  43.         else{
  44.             return 31;
  45.         }
  46.     }
  47.     void avanzarDia(){
  48.         //avanzardia por la clase
  49.         this.dia++;
  50.         if (this.dia>diasDelMes(this.mes,this.anio)){
  51.             this.dia=1;
  52.             this.mes++;
  53.             if (this.mes>12){
  54.                 this.mes=1;
  55.                 this.anio++;
  56.             }
  57.         }
  58.     }
  59.     boolean esAntesQue(Fecha otra){
  60.         //tiene que compoarar si la fecha de la clase(this) con la fecha otra
  61.         if (this.anio<otra.anio){
  62.             return true;
  63.         }
  64.         else if (this.anio>otra.anio){
  65.             return false;
  66.         }
  67.         else{
  68.                 if (this.mes>otra.mes)
  69.                     return false;
  70.                 else if (this.mes<otra.mes)
  71.                     return true;
  72.                 else{
  73.                     if (this.dia>=otra.dia)
  74.                         return false;
  75.                 return true;
  76.                     }
  77.                 }
  78.             }
  79.     int diasDeDiferenciaCon(Fecha otra){
  80.         //tiene que dar la cant de dias entre la fecha this y la fecha otra
  81.         int cont=0;
  82.         Fecha anterior, posterior;
  83.         if(this.esAntesQue(otra)){
  84.             anterior=new Fecha(this.dia,this.mes,this.anio);
  85.             posterior=new Fecha(otra.dia,otra.mes,otra.anio);
  86.         }
  87.         else{
  88.             anterior=new Fecha(otra.dia,otra.mes,otra.anio);
  89.             posterior=new Fecha(this.dia,this.mes,this.anio);
  90.         }
  91.         //se crea la fecha anterior y posterior para no modificar this.fecha
  92.         while(anterior.esAntesQue(posterior)){
  93.             cont++;
  94.             anterior.avanzarDia();
  95.         }
  96.         return cont;
  97.     }
  98.     //static metododeclase/ sin static metodo de instancia
  99.     boolean fechaValida(){
  100.         return this.mes>=1 &&
  101.             this.mes<=12 &&
  102.             this.dia>=1 &&
  103.             this.dia<=diasDelMes(this.dia,this.mes);
  104.     }
  105.     int getDia(){
  106.         return this.dia;
  107.     }
  108.     int getMes(){
  109.         return this.mes;
  110.     }
  111.     int getanio(){
  112.         return this.anio;
  113.     }
  114.     public void setAnio(int anio) {
  115.         this.anio = anio;
  116.     }
  117.     public void setDia(int dia) {
  118.         this.dia = dia;
  119.     }
  120.     public void setMes(int mes) {
  121.         this.mes = mes;
  122.     }  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement