Problem with Local Date in java

0

Good the problem that I am having with localDate is the following one, it turns out that I am modeling a system of invoicing, for this system creates 2 classes that are invoice and items, but when wanting to create an invoice with the name of the client and the date of that invoice, does not let me do it with the format 00/00/0000 since it throws me a lot of errors, therefore I do not know how to handle localDate with the format of the date.

import java.time.LocalDate;
import java.util.ArrayList;

public class Factura{

    private LocalDate fecha;
    private String nombreCliente;
    DateTimeFormatter formato = DateTimeFormatter.ofPattern("dd/MM/yyyy");
    private long precioTotal;
    public static ArrayList<Item> lista = new ArrayList<Item>();

    public Factura(String nombreCliente,LocalDate fecha){
        this.nombreCliente=nombreCliente;
        this.fecha=fecha;

    }

    //metodos get y set

    public void ponerNombreCliente(String nombreCliente){
        this.nombreCliente=nombreCliente;
    }

    public void ponerFecha(LocalDate fecha){
        this.fecha=fecha;
    }

    public void ponerPrecioTotal(long precioTotal){
        this.precioTotal=precioTotal;
    }

    public long getPrecioTotal(){
        return precioTotal;
    }

    public LocalDate getFecha(){
        return fecha;
    }

    public String getNombreCliente(){
        return nombreCliente;
    }


}
    
asked by Floppy 13.12.2018 в 16:16
source

1 answer

2

To not leave so much of the context of your code, I leave you two super simple options:

Option 1: Use what you already have

LocalDate fecha = LocalDate.now();
Factura factura = new Factura("NombreCliente", fecha);

if(factura.getFecha() != null){
    System.out.println(factura.getFecha().format(factura.formato));
} else {
    System.out.println("00/00/0000");
    // no es necesario imprimir 00/00/0000, aquí puedes colocar
    // cualquier otra lógica en caso de que la fecha sea nula
}

Always remember to validate if it is different from null to avoid errors, since in your ponerFecha method you can pass a null.

Option 2: You add an additional method to the invoice class to get the date in a string. Always validating that the date is not null.

public String getStrFecha() {
    String srtFecha = "00/00/0000";

    LocalDate fechaTemp = this.getFecha();

    if(fechaTemp != null){
        srtFecha = fechaTemp.format(this.formato);
    }

    // no es necesario asignar 00/00/0000, aquí puedes colocar
    // cualquier otra lógica en caso de que la fecha sea nula,
    // claro siempre y cuando agregues un else.

    return srtFecha;
}

And you use the getStrFecha method in this way:

LocalDate fecha = LocalDate.now();
Factura factura = new Factura("NombreCliente", fecha);

System.out.println(factura.getStrFecha());

I advise you to use option 2, so you do not have to be replicating or duplicating the if for the validation of the date everywhere, but you simply call the getStrFecha method.

If you notice the class LocalDate has a method called format which is what you lacked since the DateTimeFormatter you have in your class invoice.

I leave the link to the documentation of the method format

    
answered by 13.12.2018 / 17:54
source