Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

974
Visualizações
Timezone issue in Spring Boot JPA application

I am facing date time issue in my spring boot jpa application.

For example, In my database I have one column created_on which contains 2019-07-11 09:30:00 date. When I fetch this record threw JPA it converts to UTC.
Means date 2019-07-11 09:30:00 converts to 2019-07-11 05:00:00.

My System time is in IST and date is saved in database in IST as well.

I am using mysql database.

In my Enitity

private Date createdOn;

Database column:

created_on timestamp

Service:

@Service
@Transactional(readOnly = true)
public class EntityTypeService {
  @Autowired
  private IEntityTypeRepository entityTypeRepository;

  public EntityType findById(Long id) {
    EntityType entityType = entityTypeRepository.findById(id).orElse(new EntityType());
    System.out.println(entityType.getCreatedOn());
    return entityType;
  }
}

Repository

@Repository
public interface IEntityTypeRepository extends CrudRepository<EntityType, Long> {
}

Date in database is 2019-07-11 09:30:00

But when I print it on service System.out.println(entityType.getCreatedOn()); it gives me 2019-07-11 05:00:00.

This is generic issue in my whole application.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

After so much research I found the solution.

Actually issue was because of

spring.jpa.properties.hibernate.jdbc.time_zone=UTC

this property which I have set in my appication.properties.

When I removed this property everything works fine at the backend. Backend showing the perfect time which is available in database.

But now when response comes to frontend, at that time my date gets converted to UTC.

Like backend it's showing 2020-06-03 18:56:14.0 and when it comes to front end it converts to 2020-06-02T13:26:14.000+0000.

Which is also an issue for me.

So after some more research I found that Jackson by default converts all date to UTC when object send to frontend.

The solution to this problem is

spring.jackson.time-zone=IST

My Database and System timezone is IST so I have set IST to jackson timezone too which solves the problem.

Hope this answer may help someone.

over 4 years ago · Santiago Trujillo Relatório

0

You can set timezone for in database connection using serverTimezone eg:Asia/Kolkata and use useLegacyDatetimeCode=false

spring.datasource.url= = jdbc:mysql://127.0.0.1/db_name?useLegacyDatetimeCode=false&serverTimezone=Asia/Kolkata

And don't use legacy class Date rather use modern class LocalDateTime

over 4 years ago · Santiago Trujillo Relatório

0

Try to avoid using Date for timestamps in time zones because it is not designed to handle these in a good way.

You could try to print the date object with a formatter or use LocalDateTime (or ZonedDateTime if time zones are important to you) instead.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda