BAEL-1962 Convert ZonedDateTime for MongoDB interactions (#5640)
This commit is contained in:
+5
-1
@@ -3,6 +3,8 @@ package com.baeldung.config;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import converter.ZonedDateTimeReadConverter;
|
||||
import converter.ZonedDateTimeWriteConverter;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
@@ -52,6 +54,8 @@ public class MongoConfig extends AbstractMongoConfiguration {
|
||||
@Override
|
||||
public MongoCustomConversions customConversions() {
|
||||
converters.add(new UserWriterConverter());
|
||||
converters.add(new ZonedDateTimeReadConverter());
|
||||
converters.add(new ZonedDateTimeWriteConverter());
|
||||
return new MongoCustomConversions(converters);
|
||||
}
|
||||
|
||||
@@ -64,5 +68,5 @@ public class MongoConfig extends AbstractMongoConfiguration {
|
||||
MongoTransactionManager transactionManager(MongoDbFactory dbFactory) {
|
||||
return new MongoTransactionManager(dbFactory);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.model;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.mongodb.core.mapping.Document;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
@Document
|
||||
public class Action {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
private String description;
|
||||
private ZonedDateTime time;
|
||||
|
||||
public Action(String id, String description, ZonedDateTime time) {
|
||||
this.id = id;
|
||||
this.description = description;
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public ZonedDateTime getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public void setTime(ZonedDateTime time) {
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Action{id='" + id + "', description='" + description + "', time=" + time + '}';
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.repository;
|
||||
|
||||
import com.baeldung.model.Action;
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
|
||||
public interface ActionRepository extends MongoRepository<Action, String> { }
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package converter;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Date;
|
||||
|
||||
public class ZonedDateTimeReadConverter implements Converter<Date, ZonedDateTime> {
|
||||
@Override
|
||||
public ZonedDateTime convert(Date date) {
|
||||
return date.toInstant().atZone(ZoneOffset.UTC);
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package converter;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Date;
|
||||
|
||||
public class ZonedDateTimeWriteConverter implements Converter<ZonedDateTime, Date> {
|
||||
@Override
|
||||
public Date convert(ZonedDateTime zonedDateTime) {
|
||||
return Date.from(zonedDateTime.toInstant());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user