- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
尝试将 ZonedDateTime
与 MongoDB
一起使用。我可以将 ZonedDateTime
保存在 MongoDB
中,但是当我查看记录时,里面有很多不必要的东西:
> "timestamp" : {
> "dateTime" : ISODate("2016-12-13T13:45:53.991Z"),
> "offset" : {
> "_id" : "-05:00",
> "totalSeconds" : -18000
> },
> "zone" : {
> "_class" : "java.time.ZoneRegion",
> "_id" : "America/New_York",
> "rules" : {
> "standardTransitions" : [
> NumberLong(-2717650800)
> ],
> "standardOffsets" : [
> {
> "_id" : "-04:56:02",
> "totalSeconds" : -17762
> },
> {
> "_id" : "-05:00",
> "totalSeconds" : -18000
> }
> ],
> "savingsInstantTransitions" : [
> NumberLong(-2717650800),
> NumberLong(-1633280400),
> NumberLong(-1615140000),
> NumberLong(-1601830800),
> NumberLong(-1583690400),
> NumberLong(-1570381200),
> and so on....
此外,当我尝试检索同一日期时,它给了我以下信息:
> org.springframework.data.mapping.model.MappingException: No property
> null found on entity class java.time.ZonedDateTime to bind constructor
> parameter to!
我在使用 LocalDateTime
时没有遇到这个问题。第一个问题是我们是否可以在某个地方更改一些设置,这些设置只会将 ISODate
与 ZonedDateTime
一起保留?第二个问题,mongodb
有类似 Jsr310JpaConverters
的东西吗?
更新:引用以下问卷,我创建了自定义转换器并注册了它们,但是问题仍然存在。 Spring Data MongoDB with Java 8 LocalDate MappingException
public class ZonedDateTimeToLocalDateTimeConverter implements Converter<ZonedDateTime, LocalDateTime> {
@Override
public LocalDateTime convert(ZonedDateTime source) {
return source == null ? null : LocalDateTime.ofInstant(source.toInstant(), ZoneId
.systemDefault());
}
}
还有
public class LocalDateTimeToZonedDateTimeConverter implements Converter<LocalDateTime,
ZonedDateTime> {
@Override
public ZonedDateTime convert(LocalDateTime source) {
return source == null ? null : ZonedDateTime.of(source, ZoneId.systemDefault());
}
}
注册如下:
@Bean
public CustomConversions customConversions(){
List<Converter<?,?>> converters = new ArrayList<Converter<?,?>>();
converters.add(new ZonedDateTimeToLocalDateTimeConverter());
converters.add(new LocalDateTimeToZonedDateTimeConverter());
return new CustomConversions(converters);
}
@Bean
public MongoTemplate getMongoTemplate() throws UnknownHostException {
MappingMongoConverter converter = new MappingMongoConverter(
new DefaultDbRefResolver(getMongoDbFactory()), new MongoMappingContext());
converter.setCustomConversions(customConversions());
converter.afterPropertiesSet();
return new MongoTemplate(getMongoDbFactory(), converter);
}
最佳答案
看起来 Spring 支持除了 ZonedDateTime
转换器之外的所有 java 时间转换器。您可以通过以下方式注册一个。
@Bean
public CustomConversions customConversions(){
List<Converter<?,?>> converters = new ArrayList<>();
converters.add(new DateToZonedDateTimeConverter());
converters.add(new ZonedDateTimeToDateConverter());
return new CustomConversions(converters);
}
@Bean
public MongoTemplate getMongoTemplate() throws UnknownHostException {
MappingMongoConverter converter = new MappingMongoConverter(
new DefaultDbRefResolver(getMongoDbFactory()), new MongoMappingContext());
converter.setCustomConversions(customConversions());
converter.afterPropertiesSet();
return new MongoTemplate(getMongoDbFactory(), converter);
}
class DateToZonedDateTimeConverter implements Converter<Date, ZonedDateTime> {
@Override
public ZonedDateTime convert(Date source) {
return source == null ? null : ofInstant(source.toInstant(), systemDefault());
}
}
class ZonedDateTimeToDateConverter implements Converter<ZonedDateTime, Date> {
@Override
public Date convert(ZonedDateTime source) {
return source == null ? null : Date.from(source.toInstant());
}
}
另一种替代解决方案是仅使用 ZonedDateTime 并将其更改为日期,同时持久保存到 MongoDB。您可以在获取时轻松地将其从日期改回分区日期时间。
以下是帮助转化的相关方法。
ZoneId zoneID = ZoneId.of("America/Chicago");
从 ZonedDateTime 到 java util 日期。
Instant instant = Instant.now();
ZonedDateTime zonedDateTime = instant.atZone(zoneId);
Date date = Date.from(zdt.toInstant());
从日期到 ZonedDateTime
Instant instant = date.toInstant();
ZonedDateTime zonedDateTime = instant.atZone(zoneId);
另一种选择是实现自定义编解码器以帮助进行转换。我在从 Mongo 文档中过滤 YearMonth 时为 YearMonth 创建了一个。如果读者想为分区日期时间创建自定义编解码器,我会将其作为练习留给读者。
您可以将以下库用于基于编解码器的方法。
关于java - ZonedDateTime 与 MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41127665/
我创建了两个 ZonedDateTime 对象,我认为它们应该相等: public static void main(String[] args) { ZoneId zid = ZoneId.
假设我有一个 ZonedDateTime: ZonedDateTime zonedDateTime = ZonedDateTime.of(LocalDateTime.now(), ZoneI
给定以下单元测试: @Test public void zonedDateTimeCorrectlyRestoresItself() { // construct a new instance
我正在尝试将 ZonedDateTime 与以下代码进行比较: val now = ZonedDateTime.now() val query = for { x Timestamp.from(
我最近迁移到 Java 8,希望能更轻松地处理本地和分区时间。 但是,在我看来,在解析简单日期时,我遇到了一个简单的问题。 public static ZonedDateTime convertirA
您好,我已经搜索过类似的问题,但没有成功。我正在调用一个 ws,它将一个 token 发回给我,当它是有效的示例时: { "token": ..., "notBefore":"Thu 21
我有一个 ZonedDateTime 对象,我想根据给定的区域设置对其进行格式化。 IE。在美国,我希望格式为“M-dd-yy”,但在英国则应为“dd-M-yy”。 在给定的语言环境下执行此操作的最佳
在比较 ZonedDateTimes 时,使用 和 == 并不总是与使用 .isBefore、.isAfter 和 isEqual 相同,如下面的 Kotlin 示例所示: import java.
我有以下功能 public String getDateAndTimeInUserFormat(String value, DateTimeFormat inputFormat) { retu
我的时间以毫秒为单位,我需要将其转换为 ZonedDateTime 对象。 我有以下代码 long m = System.currentTimeMillis(); LocalDateTime d =
我尝试将字符串转换为 ZonedDateTime。这是我的代码: String startTime = "Wed Mar 21 2018 08:00:00 GMT 0330 (IRST)"; Stri
在比较 ZonedDateTimes 时,使用 和 == 并不总是与使用 .isBefore、.isAfter 和 isEqual 相同,如下面的 Kotlin 示例所示: import java.
我正在尝试获取存储为 UTC 的日期时间并将其转换为给定时区的日期时间。据我所知,ZonedDateTime 是正确的(“美国/芝加哥”比 UTC 晚 5 小时),但 DateTimeFormatte
我需要将字符串 2020-04-15T23:00:00+0000 转换为 ZonedDateTime。我尝试了以下方法,但没有成功: DateTimeFormatter formatter = Dat
如何将已设置的 ZonedDateTime 时间更改为“java.time.LocalTime.MIN”或“java.time.LocalTime.MAX”? ZonedDateTime date =
我正在尝试将字符串转换为 ZonedDateTime。 我尝试过以下操作: SimpleDateFormat zonedDateTimeFormat = new SimpleDateFormat("y
我目前正在研究日期在英国夏令时间之内和之外时的 ZonedDateTime 行为。 英国夏令时间从 3 月 25 日开始,增加一小时 (+1)。 我创建了几个 ZonedDateTime 实例(UTC
ZonedDateTime zdt3 = ZonedDateTime.parse("1999-09-09 09:09:09.999", DateTimeFormatter.of
我有一个返回年、月、日和小时的 DTO。因此将值从 DTO 传递到 ZonedDateTime.of 方法。此处,时间显示为太平洋标准时间上午 10 点。因此,尝试将其转换为 UTC。 当传递月份为
String ip="2011-05-01T06:47:35.422-05:00"; ZonedDateTime mzt = ZonedDateTime.parse(ip).toInstant().a
我是一名优秀的程序员,十分优秀!