DEV Community


Use ZonedDateTime in Spring WebFlux (MongoDB Reactive)

Thanaphoom Babparn
Software engineer who loves cloud technology. Big fans of serverless.
・Updated on ・1 min read

Hello everyone. For this article we will be empowering our Spring WebFlux and MongoDB to be able to use ZonedDateTime, so let's get started.


You can find source code in this repository

Create Spring WebFlux Project

Alt Text

And if you want to implement ZonedDateTime inside our Spring WebFlux application, you can add ReadingConverter & WritingConverter for our MongoDB. you can see the code that implementation in the below.

public class MongoConfig {

    public MongoCustomConversions mongoCustomConversions() {
        List<Converter<?,?>> converters = new ArrayList<>();
        return new MongoCustomConversions(converters);

    enum DateToZonedDateTime implements Converter<Date, ZonedDateTime> {


        public ZonedDateTime convert(Date date) {
            return date.toInstant()

    enum ZonedDateTimeToDate implements Converter<ZonedDateTime, Date> {


        public Date convert(ZonedDateTime zonedDateTime) {
            return Date.from(zonedDateTime.toInstant());
Enter fullscreen mode Exit fullscreen mode

That's it. You can use ZonedDateTime as a datetime type in our class. 😁

Example of POST request

curl --location --request POST 'http://localhost:8080/promotions' \
--header 'Content-Type: application/json' \
--data-raw '{
    "name": "promotion-2",
    "startDate": "2021-06-01T10:00:00.000+07:00",
    "endDate": "2021-06-20T18:00:00.000+07:00"
Enter fullscreen mode Exit fullscreen mode


Alt Text

Alt Text

This is a short article on how to use the converter of ZonedDateTime in Spring Data MongoDB Reactive, I hope it will be useful for all readers.

Thank you very much. πŸ˜„

Discussion (0)