简体   繁体   中英

choosing the right date format for data in json

I am working with elastic search i want to create a search by range of dates i am thinking of using LocalDate for this only problem is my date in the json data looks like this

 {
        "departure": {
            "city": "\u041c\u0438\u043d\u0441\u043a",
            "date": "Aug 27, 2018 12:09:00 AM"
        },

I have a about 1000 of such so its not wise for me to change each date in json to yyyy-mm-dd format

my search method is in java as such.

 public List<Map<String, Object>> searchDao(LocalDate departureDateFrom, LocalDate departureDateTo,
                                               String countryOrCityOrHotel, int nights, int people, String departureCity) {
        List<Map<String, Object>> search = new ArrayList<>();


        QueryBuilder range = QueryBuilders.rangeQuery("date")
                 .from(departureDateFrom)
                 .to(departureDateTo)
                .includeLower(true)
                .includeUpper(true);

        QueryBuilder cityQuery = QueryBuilders.matchQuery("departure.city", departureCity);
        QueryBuilder nightsQuery = QueryBuilders.matchQuery("nights", nights);
        QueryBuilder peopleQuery = QueryBuilders.matchQuery("people", people);
        QueryBuilder destinationHotel = QueryBuilders.matchQuery("hotel.country", countryOrCityOrHotel);

How do i change my local date format to accepts such dates so its easier to work with data as they are.

You need to use a the right mapping in your index template: { "departure.date": { "type": "date" } }

And in your java code to write this:

QueryBuilder range = QueryBuilders.rangeQuery("date")
             .from(departureDateFrom)
             .to(departureDateTo)
             .format("MM dd yyyy hh:mm:ss")
             .includeLower(true)
             .includeUpper(true);

But in general its always a good idea to use epoch time when storing dates as strings are very fragile and formats tend to change all the time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM