我有一个简单的Model&Repository类,如下所示:
StudentModel class:
@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 class:
@Repository
public interface StudentRepository extends MongoRepository<StudentModel, String> {
}
StudentTestingService class:
@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数据库中,如下面的屏幕截图所示:现在,当我查询记录(在第3行)并打印它(第4行)时,它在BST中打印(虽然在MongoDB数据库中它存储为GMT时间) .
所以,我的问题是,在“spring-data-mongodb”代码中如何以及在何处进行处理/编码这些时间转换(GMT和GMT到本地时间的位置时间)?
这看起来很基本,我相信我在这里遗失了一些东西而且输了 .
你能指点我的“spring-data-mongodb”代码库吗?如果这些转换没有在“spring-data-mongodb”中处理,那么这些转换是在哪里处理的,即它是在“mongo-java-driver”库类中?
Versions:
Spring启动版: 1.5.10
Mongo DB版本: 3.4.9