spring 数据 mongodb LocalDateTime 转换

dev*_*per 5 java spring mongodb spring-data-mongodb spring-boot

我有一个简单的模型和存储库类,如下所示:

StudentModel 班级:

@Document(collection = "student")
public final class StudentModel {

    @Id
    private final String name;

    private final LocalDateTime joiningDateTime;

    public StudentModel(String name, LocalDateTime joiningDateTime) {
        this.name = name;
        this.joiningDateTime = joiningDateTime;
    }

    public String getName() {
        return name;
    }

    public LocalDateTime getJoiningDateTime() {
        return joiningDateTime;
    }

    @Override
    public String toString() {
        return "StudentModel{" +
                "name='" + name + '\'' +
                ", joiningDateTime=" + joiningDateTime +
                '}';
    }
}
Run Code Online (Sandbox Code Playgroud)

StudentRepository 班级:

@Repository
public interface StudentRepository extends MongoRepository<StudentModel, String> {
}
Run Code Online (Sandbox Code Playgroud)

StudentTestingService 班级:

@Service
public class StudentTestingService {

    @Autowired
    private StudentRepository studentRepository;

    @PostConstruct
    public void init() {
        studentRepository.save(new StudentModel("JOHN",
                LocalDateTime.of(2018, 4, 15, 9, 30, 0)));//line1
        System.out.println(" student saved to database !!!!!!! ");//line2
        StudentModel student = studentRepository.findOne("JOHN");//line3
        System.out.println("Joining DATE :"+student.getJoiningDateTime());//line4
    }
}
Run Code Online (Sandbox Code Playgroud)

我在 Spring Boot 应用程序中运行上面的代码(服务器在 BST 时区运行)。正如您在上面看到的,我的StudentTestingService班级将joiningDateTime学生的(在 BST 中)存储为“15-APR-2018 09:30:00”(上面的第 1 行),它以 GMT 时间(即,“ 15-APR-2018 08:30:00") 如下图所示: 在此处输入图片说明 现在,当我查询记录(在第 3 行)并打印它(第 4 行)时,它会以 BST 格式打印(尽管在 MongoDB 数据库中它被存储为 GMT 时间)。

所以,我的问题是,在“spring-data-mongodb”代码中如何以及在哪里处理/编码这些时间转换(将时间定位到格林威治标准时间和格林威治标准时间再到本地时间)?

这看起来很基本,我确信我在这里遗漏了一些东西并丢失了。

你能指点我的“spring-data-mongodb”代码库吗?如果这些转换没有在“spring-data-mongodb”中处理,那么这些是在哪里处理的,即它是在“mongo-java-driver”库类中吗?

版本:

春季启动版本:1.5.10

Mongo DB 版本:3.4.9