JAVA 处理 Spring data mongodb 时区问题
2017-11-09 10:54
686 查看
Spring data mongodb 查询出结果的时候会自动 + 8小时,所以我们看起来结果是对的
但是我们查询的时候,并不会自动 + 8小时,需要自己处理
解决方法 1 @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
但是此注解,仅针对json 数据转换的时候处理,如果是form 提交 urlencoded 的时候就没办法了
虽然我们可以在 里面注册自定义的格式化,在进入Controller的时候 自动处理,但是 可能我们存在 mysql 跟 Mongodb 不同的 数据库,这种方式显然有些武断.
解决方法 2 查询Mongodb 的时候,手动处理
logback 打开debug 显示 Spring data mongodb 的查询语句,方便调试
但是我们查询的时候,并不会自动 + 8小时,需要自己处理
解决方法 1 @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
但是此注解,仅针对json 数据转换的时候处理,如果是form 提交 urlencoded 的时候就没办法了
@Transient @JsonDeserialize(using = LocalDateTimeDeserializer.class) @JsonSerialize(using = LocalDateTimeSerializer.class) @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") private LocalDateTime createDate;
虽然我们可以在 里面注册自定义的格式化,在进入Controller的时候 自动处理,但是 可能我们存在 mysql 跟 Mongodb 不同的 数据库,这种方式显然有些武断.
@InitBinder public void initBinder(WebDataBinder binder)
解决方法 2 查询Mongodb 的时候,手动处理
if (orderInfo.getCreateEndDate() != null && orderInfo.getCreateDate() != null) query.addCriteria(where("objectId").gte(new ObjectId(LocalDateTimeUtil.LocalDateTimeToUdate(orderInfo.getCreateDate().plusHours(8)))).lte(new ObjectId(LocalDateTimeUtil.LocalDateTimeToUdate(orderInfo.getCreateEndDate().plusHours(8))))); else { Optional.ofNullable(orderInfo.getCreateDate()).ifPresent(createDate -> query.addCriteria(where("objectId").gte(new ObjectId(LocalDateTimeUtil.LocalDateTimeToUdate(createDate.plusHours(8)))))); Optional.ofNullable(orderInfo.getCreateEndDate()).ifPresent(endDate -> query.addCriteria(where("objectId").lte(new ObjectId(LocalDateTimeUtil.LocalDateTimeToUdate(endDate.plusHours(8)))))); }
logback 打开debug 显示 Spring data mongodb 的查询语句,方便调试
<Logger name="org.mongodb.driver" level="debug" /> <logger name="org.springframework.data.mongodb.core.MongoTemplate" level="debug" />
{ "$gte" : { "$date" : "2017-11-01T00:00:00.000Z"}
相关文章推荐
- mongodb springdata 问题处理
- java用spring-data-mongodb操作mongodb的时间问题
- Java spring 框架跨域问题处理
- spring data mongodb的Converter问题
- spring boot 架构问题 时间处理 (映射,时区问题)
- Date, TimeZone, MongoDB, java中date的时区问题
- spring boot 架构问题 时间处理 (映射,时区问题)
- [Nosql]MongoDb+Spring [undone]java.lang.NoClassDefFoundError: org/springframework/data/mapping/context/MappingContextAware
- 云计算学习笔记---异常处理---hadoop问题处理ERROR org.apache.hadoop.hdfs.server.datanode.DataNode: java.lang.NullPoin
- Java程序与操作系统时区不一致问题的处理
- 大数据之mongodb --> (2)java集成 MongoDB 3.2,使用Spring-data-mongodb进行集成
- Hi!现在处理spring的一个问题java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool
- Java Servlet + Spring 配置中所遇问题以及处理
- Spring4.3.8+java8+mybatis3.4.4 + mariadb 时区时间问题
- java操作Mongodb,Spring-data-mongo简单配置
- 再说一下 坑爹的spring data jpa + mongodb + shiro的配置问题
- 【redis】5.spring boot项目中,直接在spring data jpa的Repository层使用redis +redis注解@Cacheable直接在Repository层使用,报错问题处理Null key returned for cache operation
- 使用Spring Data MongoDB 做聚合操作出现的问题及解决方案
- Date, TimeZone, MongoDB, java中date的时区问题
- Java 访问 MongoDB (使用Spring-Data-MongoDB)