Search code examples
jsonandroid-studioretrofit

Android retrofit change the date format "yyyy-MM-dd hh:mm:ss" in a post request


I have a problem with android and retrofit and I hope you can help me or give me a clue how to solve it. I send a post request with json to a php REST api. Dates have "yyyy-MM-dd hh:mm:ss" format because data will be stored in a mysql database. The InformeEnvio object is converted to json and the format is set with gson but in the php server dates are arriving in this format: "Oct 22, 2021 6:55:55 PM" although the json format showing in android console is this: "2021-10-22 18:55:55". An aditional thing, I´ve made a request with postman and the json string and dates arrives in the correct format. Here is the retrofit post request code and the pojo code.

Thanks.

    @Headers({
            "Accept: application/json",
            "Content-Type: application/json"
    })
    Call<PostResponse> saveInformeEnvio(@Body InformeEnvio item);


public class InformeEnvio {
    private Visita visita;
    private InformeCompra informeCompra;
    private List<InformeCompraDetalle> informeCompraDetalles;
    private List<ImagenDetalle> imagenDetalles;
    private List<ProductoExhibido> productosEx;

    public Visita getVisita() {
        return visita;
    }

    public void setVisita(Visita visita) {
        this.visita = visita;
    }

    public InformeCompra getInformeCompra() {
        return informeCompra;
    }

    public void setInformeCompra(InformeCompra informeCompra) {
        this.informeCompra = informeCompra;
    }

    public List<InformeCompraDetalle> getInformeCompraDetalles() {
        return informeCompraDetalles;
    }

    public void setInformeCompraDetalles(List<InformeCompraDetalle> informeCompraDetalles) {
        this.informeCompraDetalles = informeCompraDetalles;
    }

    public List<ImagenDetalle> getImagenDetalles() {
        return imagenDetalles;
    }

    public void setImagenDetalles(List<ImagenDetalle> imagenDetalles) {
        this.imagenDetalles = imagenDetalles;
    }

    public List<ProductoExhibido> getProductosEx() {
        return productosEx;
    }

    public void setProductosEx(List<ProductoExhibido> productosEx) {
        this.productosEx = productosEx;
    }
    public String toJson(InformeEnvio informe) {
        //  this.inf_visitasIdlocal=informe.getVisitasId();
       
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd hh:mm:ss").create();

        String informejson=gson.toJson(informe.informeCompra);
        String JSON = gson.toJson(informe);
        return  JSON;
       
      

       }
   }


Solution

  • I think you have to configure this when you create your RestAdapter. You can give a GsonConverter to retrofit that formats your dates. Something like this code should work:

    Gson gson = new GsonBuilder()
        .setDateFormat("yyyy-MM-dd hh:mm:ss")
        .create();
    
    RestAdapter restAdapter = new RestAdapter.Builder()
        .setEndpoint(API_BASE_URL)
        .setConverter(new GsonConverter.create(gson))
        .build();
    

    Depending on how you build the client, you have to use a different method:

    Retrofit retrofit = new Retrofit.Builder()
          .baseUrl(API_BASE_URL)
          .addConverterFactory(GsonConverterFactory.create(gson))
          .build();