Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package martes18;
- public class Fecha {
- //VARIABLES DE INSTANCIA
- private int dia;
- private int mes;
- private int anio;
- // se ponen en private las variables de instancia para que no puedan ser modificadas ni leidas. Para mostrarlas se necesita crear los get
- //CONSTRUCTORES
- Fecha(){
- this.dia=32;
- this.mes=1;
- this.anio=2000;
- }
- Fecha(int d,int m, int a){
- this.dia=d;
- this.mes=m;
- this.anio=a;
- }
- //METODOS
- void mostrar(){
- System.out.println(this.dia+"/"+this.mes+"/"+this.anio);
- }
- static boolean esBisiesto(int anio){
- if((anio%4==0&&anio%100!=0)||anio%400==0)
- return true;
- else
- return false;
- }
- //en los metodos static(metodosdeclase) no se puede usar el this.
- static int diasDelMes(int mes,int anio){
- //cuantos dia tiene el mes
- if (mes==4||mes==6||mes==9||mes==11){
- return 30;
- }
- if (mes==2){
- if (esBisiesto(anio)){
- return 29;
- }
- else
- return 28;
- }
- else{
- return 31;
- }
- }
- void avanzarDia(){
- //avanzardia por la clase
- this.dia++;
- if (this.dia>diasDelMes(this.mes,this.anio)){
- this.dia=1;
- this.mes++;
- if (this.mes>12){
- this.mes=1;
- this.anio++;
- }
- }
- }
- boolean esAntesQue(Fecha otra){
- //tiene que compoarar si la fecha de la clase(this) con la fecha otra
- if (this.anio<otra.anio){
- return true;
- }
- else if (this.anio>otra.anio){
- return false;
- }
- else{
- if (this.mes>otra.mes)
- return false;
- else if (this.mes<otra.mes)
- return true;
- else{
- if (this.dia>=otra.dia)
- return false;
- return true;
- }
- }
- }
- int diasDeDiferenciaCon(Fecha otra){
- //tiene que dar la cant de dias entre la fecha this y la fecha otra
- int cont=0;
- Fecha anterior, posterior;
- if(this.esAntesQue(otra)){
- anterior=new Fecha(this.dia,this.mes,this.anio);
- posterior=new Fecha(otra.dia,otra.mes,otra.anio);
- }
- else{
- anterior=new Fecha(otra.dia,otra.mes,otra.anio);
- posterior=new Fecha(this.dia,this.mes,this.anio);
- }
- //se crea la fecha anterior y posterior para no modificar this.fecha
- while(anterior.esAntesQue(posterior)){
- cont++;
- anterior.avanzarDia();
- }
- return cont;
- }
- //static metododeclase/ sin static metodo de instancia
- boolean fechaValida(){
- return this.mes>=1 &&
- this.mes<=12 &&
- this.dia>=1 &&
- this.dia<=diasDelMes(this.dia,this.mes);
- }
- int getDia(){
- return this.dia;
- }
- int getMes(){
- return this.mes;
- }
- int getanio(){
- return this.anio;
- }
- public void setAnio(int anio) {
- this.anio = anio;
- }
- public void setDia(int dia) {
- this.dia = dia;
- }
- public void setMes(int mes) {
- this.mes = mes;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement