Help with Java (POO)

1
public class CFecha {
    //ATRIBUTOS    
    int Dia;
    int Mes;
    int Año;

    //CONSTRUCTORES

    public CFecha(int pdia, int pmes, int paño){
        Dia=pdia;
        Mes=pmes;
        Año=paño;

    }

    //MODIFICADORES
    public void setDia(int pdia){
        Dia=pdia;
    }
    public void setMes(int pmes){
        Mes=pmes;
    }
    public void setAño(int paño){
        Año=paño;
    }
    //ACCEDENTES
    public int getDia(){
        return Dia;
    }
    public int getMes(){
        return Mes;
    }
    public int getAño(){
        return Año;
    }



    //OTROS MÉTODOS

    public CFecha FechaMayor(CFecha fecha)
    {
    int Dia1=fecha.getDia();
    int Mes1=fecha.getMes();
    int Año1=fecha.getAño();

    if(Año>Año1)    
      return this;


    else
        if(Año<Año1)    
            return fecha;
        else   
        if(Año==Año1)        
        {
            if(Mes>Mes1)     
                return this;        
            else 
                if(Mes<Mes1)
                    return fecha;
                else
                    if(Dia>Dia1)
                        return this;
                    else
                        return fecha;       

        }
    return null;
    }
    public void EscribeFecha(){
        System.out.println("La fecha mayor es: "+Dia +"/"+ Mes+"/"+ Año);
    }

}

How can I show how many days have that month (if I have a date in the main class, I want to know how many)?

    
asked by Elvis Aldair Anaya Mendoza 05.12.2016 в 05:38
source

2 answers

2

It is best to define a method like the following:

public static int getDiasEnMes(int mes, int año) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, año);
    calendar.set(Calendar.MONTH, mes - 1); // Índice del mes, empezando en 0
    return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}

This method creates a Calendar object with the mes and the año specified and gets the last day of that mes , or what is the same, the total number of days of that mes .

Notice that to assign the mes the argument that the function receives is subtracted 1. This is because for calendar , January is month 0, February 1, March 2 ...

Take a look at the documentation: link

    
answered by 05.12.2016 в 11:33
0

Since I do not know exactly how you are going to provide the data, I'll give you the most complete solution, take what you need:

SimpleDateFormat dateFormat = new SimpleDateFormat("d/M/yyyy");
//Para introducir los datos con una cadena con el formato anterior
Date convertedDate = dateFormat.parse(date);
Calendar c = Calendar.getInstance();
//Para introducir los datos con una fecha tipo java.util.Date
c.setTime(convertedDate);
c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));

You can see the easy way out to make sure you do what you want:

dateFormat = new SimpleDateFormat("d");
System.out.println(dateFormat.format(c.getTime()));
    
answered by 05.12.2016 в 09:12