Java Date Time – How to build SpringBoot RestApi – Post/Get request with Java Date Time using Jackson and Make Query with Spring JPA example


In the tutorial, we build a SpringBoot RestAPIs example that post/get data with java.util.Date time and save it to MySQL/PostgreSQL database using Spring JPA. Working with Java Date Time is an exciting part but also not easy task, fortunately we have the supporting from utilities of Jackson lib, now the job can be done in an easy way.

Let’s do details by steps!

Format Java Date Time with Jackson

Set the Format with @JsonFormat

With the @JsonFormat annotation of Jackson, we can use it to format a specific field in Java model:

Set TimeZone with @JsonFormat

For setting Time Zone, we use timezone attribute of the @JsonFormat:

Set Default Format

We can configure a default format & time-zone for all dates in

Map Date and Time with Spring JPA

We use the @Temporal annotation to specify what the field represents. This annotation goes with a parameter having a value of TemporalType enum:

-> Now, our model will be like:

Query the Entities with Date Time property from Database

For query data with Date Time property from database, we need to implement CrudRepository and use 2 approaches.

– Use the built-in rule of Spring JPA mechanics:

– Use @Query annotation:

RequestParam with Date Time in RestApi

For adding a request param with Date Time data type in RestAPI, we use @RequestParam & @DateTimeFormat


Create SpringBoot Project

We create a SpringBoot project as below structure:


– Dependencies List:

Create Dates & Times Model

– Create model:

Create Repository

– Create repository:


– Create restapis:

Database Configuration

Config MySQL

Config PostgreSQL

Run & Check Result

Run SpringBoot project, then makes requests.

– Post request:




– Get All Entities request:


– Query Request ->

+ Get Entities between 2 Date Time:


+ Get Entities before a Date Time:





Through the tutorial, We had learned how to create a SpringBoot RestAPI with Java Date Time data:

  • Using @JsonFormat annotation to format Date Time
  • Using @Temporal annotation to map Date and Time to work with Spring JPA
  • Query the Entities with Date Time property from Database
  • Format RequestParam with Date Time type in RestAPI by using @DateTimeFormat

Thank you for reading! See you next time!

By grokonez | March 25, 2019.

Related Posts

2 thoughts on “Java Date Time – How to build SpringBoot RestApi – Post/Get request with Java Date Time using Jackson and Make Query with Spring JPA example”

  1. Hi , Its an awesome blog on DAte and time but I am confused in repository section. can u explain how it is implemented .
    i need further explanation in those 2 approaches used in DATETimeReposirory!

    Waiting for your reply!

Got Something To Say:

Your email address will not be published. Required fields are marked *