Spring @RequestParam DateTime format as ISO 8601 Date Optional Time

ystark

I'm using Spring Framework for my services API and org.joda.time.DateTime for datetime parsing. Specifically, I'm using the ISOFormatter.dateOptionalTimeParser(), which allows users the flexibility to use just the date, or both date and time, which is a requirement.

Believe me, I've seen all these related questions that I can already tell people are going to point me towards, e.g. this and this, etc.

Previously, I was taking the date as String and then processing it using the joda formatter mentioned above in the service layer, but now I want to add request validation in the controller, which means that if the request is syntactically incorrect, the request shouldn't even go to the service layer.

I've tried using multiple variations of @DateTimeFormat(iso = ISO.DATE_TIME), as well as specifying the pattern String in format thing with no luck, whatsoever.

@RequestMapping(value = URIConstants.TEST_URL, method = RequestMethod.GET)
public @ResponseBody String getData(@RequestParam(required = false) DateTime from,
                                    @RequestParam(required = false)  DateTime to)  {
    return dataService.fetchDataFromDB(from, to);
}

What should I do to ensure that the date I get from user complies with the ISO 8601 dateOptionalTime format? Can I maybe apply multiple patterns to implement this?

divinedragon

You can also create a converter and that will take care of it. I have used OffsetDateTime in the example below, but that can be easily replaced with LocalDateTime. For a detailed article, refer this url - http://www.baeldung.com/spring-mvc-custom-data-binder

Even I was struggling with this for sometime and it wasn't working. The trick is to use the @Component annotation and did it for me.

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

@Component
public class OffsetDateTimeConverter implements Converter<String, OffsetDateTime> {

    @Override
    public OffsetDateTime convert(final String source) {

        if (source == null || source.isEmpty()) {
            return null;
        }

        return OffsetDateTime.parse(source, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
    }
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Spring @RequestParam DateTime format as ISO 8601 Date Optional Time

分類Dev

Formatter in DateTimeFormatter for ISO 8601 date format of the time

分類Dev

Epoch or iso8601 date format?

分類Dev

Converting a time String to ISO 8601 format

分類Dev

How to parse and generate DateTime objects in ISO 8601 format

分類Dev

Deserialize "Zulu" time in ISO8601 format in jackson

分類Dev

PHP $date->format(DateTime::ISO8601) 異なるタイムゾーンオフセットを返す

分類Dev

How do I convert a Calendar object to an ISO 8601 format DateTime string?

分類Dev

BigQuery-iso8601の週のformat_dateと年

分類Dev

ISO 8601DateTime表現

分類Dev

How to parse a ISO 8601 duration format in Swift?

分類Dev

Parsing ISO 8601 date format like 2015-06-27T13:16:37.363Z in Java

分類Dev

Parsing ISO 8601 date format like 2015-06-27T13:16:37.363Z in Java

分類Dev

JodaTime DateTime、ISO8601 GMT日付形式

分類Dev

Java Jsonb deserializing UTC datetime in ISO8601

分類Dev

Pandas read_csv not recognizing ISO8601 as datetime dtype

分類Dev

Parsing ISO 8601 duration format to Joda duration - IllegalArgumentException

分類Dev

How to parse input string in standard ISO 8601 format in UTC to Java?

分類Dev

Return dates in ISO-8601 format - Debezium/Postgres plugin

分類Dev

ISO8601 Date Strings in MongoDB (Indexing and Querying)

分類Dev

Java 8 Date&Time API:特定の月のISO8601週のカウントを取得する方法は?

分類Dev

What is this date and time format?

分類Dev

Converting date and time from String format to Python datetime object: ValueError: time data '... p.m.' does not match format '... %p'

分類Dev

オプション付きのLocalDateTimeにISO_DATE_TIME.format()オフセット

分類Dev

Does ISO-8601 allow the time-zone abbreviation UTC rather than Z as the time-zone?

分類Dev

正規表現およびISO8601形式のDateTime

分類Dev

ISO8601のDateTime + TimeZoneおよびマイクロ秒なし

分類Dev

DateTimeをISO8601に変換します

分類Dev

Parse Date and Time in specific format

Related 関連記事

  1. 1

    Spring @RequestParam DateTime format as ISO 8601 Date Optional Time

  2. 2

    Formatter in DateTimeFormatter for ISO 8601 date format of the time

  3. 3

    Epoch or iso8601 date format?

  4. 4

    Converting a time String to ISO 8601 format

  5. 5

    How to parse and generate DateTime objects in ISO 8601 format

  6. 6

    Deserialize "Zulu" time in ISO8601 format in jackson

  7. 7

    PHP $date->format(DateTime::ISO8601) 異なるタイムゾーンオフセットを返す

  8. 8

    How do I convert a Calendar object to an ISO 8601 format DateTime string?

  9. 9

    BigQuery-iso8601の週のformat_dateと年

  10. 10

    ISO 8601DateTime表現

  11. 11

    How to parse a ISO 8601 duration format in Swift?

  12. 12

    Parsing ISO 8601 date format like 2015-06-27T13:16:37.363Z in Java

  13. 13

    Parsing ISO 8601 date format like 2015-06-27T13:16:37.363Z in Java

  14. 14

    JodaTime DateTime、ISO8601 GMT日付形式

  15. 15

    Java Jsonb deserializing UTC datetime in ISO8601

  16. 16

    Pandas read_csv not recognizing ISO8601 as datetime dtype

  17. 17

    Parsing ISO 8601 duration format to Joda duration - IllegalArgumentException

  18. 18

    How to parse input string in standard ISO 8601 format in UTC to Java?

  19. 19

    Return dates in ISO-8601 format - Debezium/Postgres plugin

  20. 20

    ISO8601 Date Strings in MongoDB (Indexing and Querying)

  21. 21

    Java 8 Date&Time API:特定の月のISO8601週のカウントを取得する方法は?

  22. 22

    What is this date and time format?

  23. 23

    Converting date and time from String format to Python datetime object: ValueError: time data '... p.m.' does not match format '... %p'

  24. 24

    オプション付きのLocalDateTimeにISO_DATE_TIME.format()オフセット

  25. 25

    Does ISO-8601 allow the time-zone abbreviation UTC rather than Z as the time-zone?

  26. 26

    正規表現およびISO8601形式のDateTime

  27. 27

    ISO8601のDateTime + TimeZoneおよびマイクロ秒なし

  28. 28

    DateTimeをISO8601に変換します

  29. 29

    Parse Date and Time in specific format

ホットタグ

アーカイブ