Spring @RequestParam DateTime格式为ISO 8601日期可选时间 [英] Spring @RequestParam DateTime format as ISO 8601 Date Optional Time

查看:110
本文介绍了Spring @RequestParam DateTime格式为ISO 8601日期可选时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Spring Framework用于我的服务API,并将org.joda.time.DateTime用于日期时间解析.具体来说,我使用的是ISOFormatter.dateOptionalTimeParser(),它使用户可以灵活地仅使用日期,或者同时使用日期和时间,这是必须的.

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.

以前,我将日期作为String,然后在服务层中使用上面提到的joda格式化程序对其进行处理,但是现在我想在控制器中添加请求验证,这意味着如果请求在语法上不正确,该请求甚至都不应转到服务层.

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.

我尝试使用@DateTimeFormat(iso = ISO.DATE_TIME)的多个变体,以及以格式形式指定pattern字符串,但没有任何运气.

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);
}

我该怎么做才能确保我从用户那里得到的日期符合ISO 8601 dateOptionalTime格式?我可以运用多种模式来实现吗?

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?

推荐答案

您还可以创建一个转换器,它将进行处理.我在下面的示例中使用了OffsetDateTime,但是可以轻松地将其替换为LocalDateTime.有关详细的文章,请参考以下网址- http://www.baeldung.com/spring-mvc-custom-data-binder

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

即使我为此苦了一段时间,也没有用.诀窍是使用@Component批注为我完成.

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);
    }
}

这篇关于Spring @RequestParam DateTime格式为ISO 8601日期可选时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆