Mysql returns Localdate a day ago

Asked

Viewed 211 times

0

I am unable to identify a problem using Java Localdate with Mysql Date. When saving a date in my Mysql database it saves on the correct date, for example: 2019-05-24.

When doing a search using criteriaQuery, it finds the expected record on the date, but is returning me the day before. I thought it might be the way I’m displaying the date in my converter:

@FacesConverter("localDateFacesConverter")
public class LocalDateFacesConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String stringValue) {
        if (null == stringValue || stringValue.isEmpty()) {
            return null;
        }
        LocalDate localDate = null;
        try {
            localDate = LocalDateHelper.strToLocalDate(stringValue.trim(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        } catch (DateTimeParseException e) {
            throw new ConverterException("O formato da data deve ser 2015/11/15");
        }
        return localDate;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object instant) {
        if (null == instant) {
            return "";
        }
        if (component instanceof javax.faces.component.html.HtmlOutputText) {
            if (instant instanceof LocalDate) {
                return LocalDateHelper.formataLocalDate((LocalDate) instant, DateTimeFormatter.ofPattern("dd/MM/yyyy"));
            } else if (instant instanceof LocalDateTime) {
                return LocalDateHelper.formataLocalDateTime((LocalDateTime) instant, DateTimeFormatter.ofPattern("dd/MM/yyyy"));
            }
            return LocalDateHelper.formataInstant((Instant) instant, DateTimeFormatter.ofPattern("dd/MM/yyyy"));
        }
        if (instant instanceof LocalDate) {
            return LocalDateHelper.formataLocalDate((LocalDate) instant, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        } else if (instant instanceof LocalDateTime) {
            return LocalDateHelper.formataLocalDateTime((LocalDateTime) instant, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }
        return LocalDateHelper.formataInstant((Instant) instant, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }
}

I’ve tried everything, but I can’t seem to solve the problem. Someone’s been there and they have a solution?

  • 1

    Probably a time zone problem.

  • @anonimo Because I also thought it would be, but it’s not, all configured for the correct time zone, at all times, in the bank, in the application server and in the code.

  • @anonimo I kept looking for a possible problem with the time zone and found. Thanks for the help!

1 answer

0

After performing several tests, I found reports about the same problem and solved it by configuring the Mysql time_zone.

set @@time_zone = '-03:00'
set @@session.time_zone = '-03:00'

it is necessary to add the default-time-zone in the Mysql settings file so that when starting this is the adopted default. The file is in (/etc/my.cnf) add the following property at the end of the file:

default-time-zone='-03:00'

I also configured the Glassfish DS URL property as follows:

  <property name="URL" value="jdbc:mysql://127.0.0.1:3306/conciliador?autoReconnect=true&amp;useSSL=false&amp;useLegacyDatetimeCode=false&amp;serverTimezone=America/Sao_Paulo"></property>

With that came to work perfectly.

Browser other questions tagged

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