spring data mongodb LocalDateTime转换

时间:2018-04-18 13:32:26

标签: java spring mongodb spring-boot spring-data-mongodb

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

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 +
                '}';
    }
}

StudentRepository课程:

@Repository
public interface StudentRepository extends MongoRepository<StudentModel, String> {
}

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
    }
}

我在Spring启动应用程序中运行上面的代码(服务器在BST时区运行)。 如您所见,我的StudentTestingService课程将学生joiningDateTime(在BST中)存储为“15-APR-2018 09:30:00”(上面第1行) 使用GMT时间(即“15-APR-2018 08:30:00”)保存在MongoDB数据库中,如下面的屏幕截图所示: enter image description here 现在,当我查询记录(在第3行)并打印它(第4行)时,它会在BST中打印(虽然在MongoDB数据库中它存储为GMT时间)。

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

这似乎很基本,我相信我在这里错过了一些东西。丢失。

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

版本:

Spring boot版本: 1.5.10

Mongo数据库版本: 3.4.9

0 个答案:

没有答案
相关问题