Incorrect date arriving

Asked

Viewed 123 times

1

I have the following problem here: I have a request that I make by ajax, and send a date for it, the java receives and saves in the bank, but the date that arrives in java is different from the one I sent. Follow the code:
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');
    }
});

Controller method:

@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);
    }

}

Data in the model (I was using without the Datetimeformat annotation, and it didn’t work either, I put it to see if it worked, it didn’t work):

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

I’m taking the date of the bootstrap datepicker, but the date coming out of it is correct:

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

But the date that arrives in java is another: inserir a descrição da imagem aqui

Can anyone help me? Thank you!

1 answer

0

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

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

On the client side I do the conversion and later formatting:

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: https://blog.serverdensity.com/automatic-timezone-conversion-in-javascript/ https://stackoverflow.com/questions/948532/how-do-you-convert-a-javascript-date-to-utc http://www.epochconverter.com

Browser other questions tagged

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