Date arriving incorrect

1

I have the following problem here: I have a request that I make through ajax, and I send a date for it, java receives and saves it in the database, but the date that arrives in java is different from the one I sent. Follow the codes: Home Ajax request:

var episodio = {
    titulo  : $('#inputTitulo').val(),
    descricao : $('#textSinopse').val(),
    sneakPeak : $('#inputTrailer').val(),
    dataEstreia : $('#inputDataEstreia').val(),
    numero  : $('#inputNumero').val(),
    duracao : $('#inputDuracao').val()
};  

$.ajax({
    url: '/seriesmais/ajax/episodio/',
    dataType: 'json',
    method: 'POST',
    data: episodio,
    success: function(episodioRecebida) {
        alert('inseriu');
    }
});

Method not controller:

@RequestMapping(value = "/", method = RequestMethod.POST)
public ResponseEntity<Object> salvar(Episodio episodio) {
    System.out.println(episodio);

    try {

        if (episodio.getId() == null){
            System.out.println("DATA: " + episodio.getDataEstreia());
            daoEpisodio.inserir(episodio);
        } else {
            daoEpisodio.alterar(episodio);
        }
        return ResponseEntity.status(HttpStatus.OK).body(episodio);
    } catch (Exception e) {
        e.printStackTrace();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

}

Date in the model (I was using it without the DateTimeFormat annotation, and it did not work either, I put it to see if it worked, it did not work):

@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(pattern = "dd/MM/yyyy")
private Date dataEstreia;

I'm getting the bootstrap datepicker date, but the date it exits is correct:

$('.datepicker').datepicker({weekStart:1,format:'dd/mm/yyyy'});

Butthedatethatarrivesinjavaisanother:

Can anyone help me? Thanks!

    
asked by anonymous 21.12.2016 / 22:10

1 answer

0

I usually avoid sending formatted dates as parameters or response requests. I always use the UTC Timestamp format and do the conversion / formatting on the client side. This avoids a lot of problems.

For example, if I want to send the date 22/12/2016 11:54:59 in response, I send that date in UTC format (1482407699000).

On the client side I convert and later format:

getDate: function(timestamp)
{
// Multiply by 1000 because JS works in milliseconds instead of the UNIX seconds
var date = new Date(timestamp * 1000);

var year = date.getUTCFullYear();
var month = date.getUTCMonth() + 1; // getMonth() is zero-indexed, so we'll increment to get the correct month number
var day = date.getUTCDate();
var hours = date.getUTCHours();
var minutes = date.getUTCMinutes();
var seconds = date.getUTCSeconds();

month = (month < 10) ? '0' + month : month;
day = (day < 10) ? '0' + day : day;
hours = (hours < 10) ? '0' + hours : hours;
minutes = (minutes < 10) ? '0' + minutes : minutes;
seconds = (seconds < 10) ? '0' + seconds: seconds;

return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes;
}

References: link link link

    
22.12.2016 / 13:03