简体   繁体   中英

Mongo db date Saving issues

Trying to save Date in Mongo db in java apllication but it is saving one day before i have tried code mentioned below. I tried creating a custom conversion using Zonal Date Time Converter please help if someone has faced issue like that.

 @Bean
public CustomConversions customConversions(){
    List<Converter<?,?>> converters = new ArrayList<Converter<?,?>>();
    converters.add(new DateToZonedDateTimeConverter());
    converters.add(new ZonedDateTimeToDateConverter());
    return new CustomConversions(converters);
}

@Bean
public MongoTemplate getMongoTemplate() throws UnknownHostException {
    MappingMongoConverter converter = new MappingMongoConverter(
            new DefaultDbRefResolver(getMongoDbFactory()), new MongoMappingContext());
    converter.setCustomConversions(customConversions());
    converter.afterPropertiesSet();
    return new MongoTemplate(getMongoDbFactory(), converter);
}

class DateToZonedDateTimeConverter implements Converter<Date, ZonedDateTime> {

     @Override
     public ZonedDateTime convert(Date source) {
              return source == null ? null : ofInstant(source.toInstant(), systemDefault());
         }
     }

class ZonedDateTimeToDateConverter implements Converter<ZonedDateTime, Date> {

    @Override
    public Date convert(ZonedDateTime source) {
             return source == null ? null : Date.from(source.toInstant());
       }
   }

MongoDB will convert your date to GMT time during saving. But if you pull your date again to local time zone you will have appropriate time.

If you have application intended for different geo locations with different zones you could use GMT time zone on backend and local time zone on frontend. If zone is important for your business case you can save in mongodb both GMT time and zone separately.

You will not be able to save zone inside date object in MongoDB. Its just looks bad but its good and I will try to explain.

Suppose you want to save date from:

  • Germany/Berlin as local date 2019-09-20T00:20:00.000 GMT+1
  • MongoDB will convert this date to GMT and you will see in database ISODate("2019-09-19T23:20:00.000Z") (look like one day before same as you describe)

If you fetch this mongo document and deserialize to Java object with date you could print date and you will see:

  • In Germany and GMT+1 locations you will print what you expect 2019-09-20T00:20:00.000 GMT+1 because Java java.util.Date automatically convert GMT to local time
  • In other locations you will see other times for example in London you will see 2019-09-19T23:20:00 GMT and its ok because London is 1 hour before Berlin

Solution depending of your case but common mistake is date picker which provide local date with time 00:00 and conversion create problems. In this case just send UTC date from frontend side. You have 2 common cases:

  • You need to save time of online call (you don't need location, some members will be from Japan, some from Germany and some from Brasil) In this case you could send local time from your time picker and mongo will convert to GMT. When clients fetch dates you will return UTC and they will have automatic conversion to local times and all clients will see correct time
  • You need to save time of face to face appointment (you need to save location because some members will be from Japan, some from Germany and some from Brasil and suppose they have to know what is local time) In this case you could save date in one field and tiemzone in second filed. This will ensure what you want.

If you have some another case or some another problem please share more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM