在 spring boot jpa 中存储日期的最佳方式是什么?
What's the best way to store date in spring boot jpa?
在 spring 引导实体中存储数据库日期的最佳方式是什么?
例如,在我的数据库中,我需要像 2021-02-19 17:00
这样存储 smth,然后在我的 spring 启动应用程序
中构建此日期的查询
如果你想将日期作为字符串存储在你的数据库中,你有很多选择,但我认为回答哪个更好完全取决于你的数据库和你的映射策略,例如考虑
DateTimeFormatter firstFormatter = DateTimeFormatter.ofPattern("dd, MMM, yyyy", Locale.ENGLISH);
LocalDate date = LocalDate.of(2021, Month.NOVEMBER, 7);
String firstDateAsString = firstFormatter.format(date);
System.out.println(firstDateAsString);
第一个firstFormatter.formate()
接受TemporalAccessor,作为另一个例子
DateFormat secondFormatter = new SimpleDateFormat("dd MM yyyy");
String secondDateAsString= secondFormatter.format(new Date());
System.out.println(secondDateAsString);
请注意,hibernate 查询负载和执行时间将完全取决于您映射和标记化按日期累积的字符串。
在数据库中使用 LocalDateTime 和适当的日期列类型。然后,如果需要,您将能够在数据库查询和 Java 中轻松地进行日期数学运算(例如比较)。
在 spring 引导实体中存储数据库日期的最佳方式是什么?
例如,在我的数据库中,我需要像 2021-02-19 17:00
这样存储 smth,然后在我的 spring 启动应用程序
如果你想将日期作为字符串存储在你的数据库中,你有很多选择,但我认为回答哪个更好完全取决于你的数据库和你的映射策略,例如考虑
DateTimeFormatter firstFormatter = DateTimeFormatter.ofPattern("dd, MMM, yyyy", Locale.ENGLISH);
LocalDate date = LocalDate.of(2021, Month.NOVEMBER, 7);
String firstDateAsString = firstFormatter.format(date);
System.out.println(firstDateAsString);
第一个firstFormatter.formate()
接受TemporalAccessor,作为另一个例子
DateFormat secondFormatter = new SimpleDateFormat("dd MM yyyy");
String secondDateAsString= secondFormatter.format(new Date());
System.out.println(secondDateAsString);
请注意,hibernate 查询负载和执行时间将完全取决于您映射和标记化按日期累积的字符串。
在数据库中使用 LocalDateTime 和适当的日期列类型。然后,如果需要,您将能够在数据库查询和 Java 中轻松地进行日期数学运算(例如比较)。