简体   繁体   English

Spring Data Mongo:如何仅保存日期,即不保存时间

[英]Spring Data Mongo: How to save only date i.e. not time

In one of the collections I have a field that need to be set to the current date, for example '2018-01-26'. 在其中一个集合中,我有一个字段需要设置为当前日期,例如“ 2018-01-26”。 It should not include any time in it. 它不应包含任何时间。 This makes it easy for me to query the documents based on a date. 这使我可以轻松地根据日期查询文档。 I have tried with the below code and it is always saving date with time in it. 我尝试使用下面的代码,它总是在其中节省时间。

 @DateTimeFormat(iso= DateTimeFormat.ISO.DATE_TIME)
private Date journeyDate;

or 要么

 @DateTimeFormat(iso= DateTimeFormat.ISO.DATE)
    private Date journeyDate;

How can only save the date part in the database? 如何只将日期部分保存在数据库中?

You cannot really set the date in Mongo as '2018-01-26'. 您实际上无法在Mongo中将日期设置为“ 2018-01-26”。 Because whatever conversion we do, in the end we need to create a Date object to save in Mongo. 因为无论我们进行什么转换,最终我们都需要创建一个Date对象以保存在Mongo中。 And a date object cannot exists without time. 没有时间就不可能存在日期对象。

However what you can do is, you can control what is stored by writing a custom converter and registering it. 但是,您可以做的是,您可以通过编写自定义转换器并注册来控制存储的内容。
In this converter what I am doing is truncating all the date to start of the day, that means the time for all the dates is set to 00:00:00(irrespecitve of their time). 在此转换器中,我正在做的是将所有日期都截断到一天的开始,这意味着所有日期的时间都设置为00:00:00(与时间无关)。 This way you have same time for all days. 这样,您一整天都有相同的时间。 I hope this would help in writing the query you need. 我希望这将有助于编写您需要的查询。

@Configuration
public class MongoConverterConfig
{

  @Bean
  public CustomConversions dateConversions()
  {
    List<Converter<?, ?>> converterList = new ArrayList<Converter<?, ?>>();
    converterList.add(new DateConverter());
    return new CustomConversions(converterList);
  }

  @WritingConverter
  static class DateConverter implements Converter<Date, Date> {

    @Override
    public Date convert(Date input) {
      if (input == null) {
        return null;
      }
      return Date.from(Instant.ofEpochMilli(input.getTime()).truncatedTo(ChronoUnit.DAYS));
    }
  }
}

If you really want to store just '2018-01-26' in mongo, then you will have to store it as a String, which I wouldn't recommend. 如果您真的只想在mongo中存储“ 2018-01-26”,那么您将不得不将其存储为String,我不建议这样做。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在没有时间的情况下将 LocalDate 保存到 MongoDB(为什么即使我只保存日期,mongo 也会随时间保存日期)? - How to save LocalDate to MongoDB without time (why is mongo saving date with time even if i save just the date)? 如何一次调用两个参与者,即并行? - How to invoke two actors at a time , i.e. parallel? Java创建dayOfWeek的日期和没有特定日期的时间,即对任何星期一有效 - java create date for dayOfWeek and time without a specific date, i.e. valid for any Monday 如何使用Spring转换服务将字符串值从属性文件自动转换为其他数据类型,即持续时间? - How to auto convert string values from properties files to other data types i.e. duration using spring conversion service? 在Spring Security 5中仅对某些特定的URL允许分号,即StrictHttpFirewall? - Allow semicolon only for some specific url in Spring Security 5 i.e. StrictHttpFirewall? 在 Android Wear OS(即 CSV)上保存和传输智能手表传感器数据的最佳方式是什么? - What is the best way to save and transfer smartwatch sensor data on Android Wear OS (i.e. as CSV)? Spring Data JPA:为什么保存该实体会按预期进行(即,很高兴父级不会被覆盖)? - Spring Data JPA: Why does saving this entity work as intended (i.e. parent is thankfully not overwritten)? 如何在 Android Studio 的所有页面上保存功能(即 SFX 或音乐等设置)? - How to save a feature (i.e. setting like SFX or Music) across all pages in Android Studio? 如何创建并使用字符串作为输入输出Date对象? 即“ 13/04/1998” - How do I create and then output a Date object using a String as the input? i.e. “13/04/1998” 如何在 Spring MVC 中捕获所有未处理的异常(即没有现有的 @ExceptionHandler)? - How to catch all unhandled exceptions (i.e. without existing @ExceptionHandler) in Spring MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM