Public method where a date is passed as a parameter, a parameter only

Asked

Viewed 140 times

2

I need to create a public method called mostraData where a date is passed as a parameter (a parameter, not a separate day, month and year) and the date is returned in the following format: dd / mm /aaaa .

Keep in mind that if the values have fewer digits, you must fill in with zeros on the left to comply with the standard format. I already created the builder as I was asked in the exercise (one by default and another by parameter) with the requested attributes as well. Thank you.

package fechaapp;

public class Fecha {
private int dia;
private int mes;
private int anyo;

public Fecha(){
    this.setDia(1);
    this.setMes(1);
    this.setAnyo(2000);
}

public Fecha(int dia, int mes, int anyo){
    this.setDia(dia);
    this.setMes(mes);
    this.setAnyo(anyo);
}

public int getDia() {
    return dia;
}

public void setDia(int dia) {
    this.dia = dia;
}

public int getMes() {
    return mes;
}

public void setMes(int mes) {
    this.mes = mes;
}

public int getAnyo() {
    return anyo;
}

public void setAnyo(int anyo) {
    this.anyo = anyo;
}
public void mostraData(int fecha){

}
public void calculaSemanaMes(){}
public void calculaSemanaAnyo(){}
public void esAnyoExtraordinario() {}
public void calculaDiaSemana(){}
public void esFestivo(){}
public void esLaborable(){}
public void fechaFormat(){}
}
  • what is the pattern of the date you received? ddmmaaaa?

  • Yeah, that’s right, I’m sorry.

2 answers

0

Thus:

int numero = 20102015
char[] digitos = String.valueOf( numero ).toCharArray();

//desta forma, digito[0]+digito[1] = 20, digito[2]+digito[3] = 10, digito[4]+digito[5]+digito[6]+digito[7] = 2015;
//Dae, so vc atribuir na respectiva variavel.
  • Thanks, but what if the user type 2012015 or 1122015? How to know what is the day and what is the month? I was wondering if I could force the user to type / (1/02/2015 for example) and then take the /, what do you think? Is it possible?

  • what Voce needs to connect is in the pattern, if it is an interface, Voce can use a mask, forcing the bars and the size, and would be 20/10/2015, in this case you would receive a String, and it would always be the default. It would be ideal, Voce can give a String[] dates = str.split("/"), where each date would be a field... dates[0]=day, dates[1]=month and dates[2]=year... But to receive without Exception, you must prevent the format in the input.... This example that I passed is simple to understand...

  • Blz, I got it, thank you!

0


You can use String#format() to include zeros on the left.

String foo = String.format("%08d", 1234); // 00001234

In this example, a formatted String of size will be returned 8 where, to complete the missing size (in this case, eight) 0 will be inserted on the left. Some examples:

String.format("%010d", 123); //0000000123 (incluiu 7 zeros, tamanho 10)
String.format("%05d", 123);  //00123      (incluiu 2 zeros, tamanho 5 )
String.format("%010d", 1);   //0000000001 (incluiu 9 zeros, tamanho 10)

So your method can be like this:

public String mostraData(String data) {
    String[] array = data.split("/");

    // Não é uma entrada x/x/x, então retorna a própria data
    if (!(array.length == 3)) return data;

    data = "";
    for (String each : array)
        data += String.format("%02d/", Integer.parseInt(each));
    // remove a última "/" do String#format antes de retornar
    return data.substring(0, data.length() -1);
}

When called:

mostraData("10/2");      // Mostra a própria data inserida: 10/2
mostraData("10/2/2015"); // 10/02/2015
mostraData("5/2/2015");  // 05/02/2015
mostraData("10/12/15");  // 10/12/15

Example in Ideone

Browser other questions tagged

You are not signed in. Login or sign up in order to post.