Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Date Jersey2定制日期转换器_Date_Jersey 2.0_Epoch_Iso8601 - Fatal编程技术网

Date Jersey2定制日期转换器

Date Jersey2定制日期转换器,date,jersey-2.0,epoch,iso8601,Date,Jersey 2.0,Epoch,Iso8601,有人为Jersey2创建了CustomConvertor类进行转换吗 ISO 8601日期到日期/纪元时间 我以ISO_8601_格式=“yyyy-MM-dd'T'HH:MM:ss.SSSZ”将日期作为查询参数传递;我需要转换成秒。我有所有必要的东西,我在把它粘起来时迷失了方向。 我希望自定义转换器在我们看到DateEpochMarker接口后立即启动。我用运动衫2 我错过了哪一步 谁能帮我一下吗 我有一个客户转换器,一个标记接口和资源方法 public class DateToEpochCon

有人为Jersey2创建了CustomConvertor类进行转换吗 ISO 8601日期到日期/纪元时间

我以ISO_8601_格式=“yyyy-MM-dd'T'HH:MM:ss.SSSZ”将日期作为查询参数传递;我需要转换成秒。我有所有必要的东西,我在把它粘起来时迷失了方向。 我希望自定义转换器在我们看到DateEpochMarker接口后立即启动。我用运动衫2

我错过了哪一步

谁能帮我一下吗

我有一个客户转换器,一个标记接口和资源方法

public class DateToEpochConvertor implements ParamConverter<Long> {
    private static final String ISO_8601_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
    @Override
    public Long fromString(String value) {
        DateFormat df1 =  new SimpleDateFormat(ISO_8601_FORMAT);

        Date date = new Date();
        try {
            date = df1.parse(value);
        } catch (ParseException e) {
            throw new WebApplicationException("The Date "+value+" is not in the ISO 8601 Format ");
        }

        return date.getTime();
    }

    @Override
    public String toString(Long value) {
        DateFormat df1 =  new SimpleDateFormat(ISO_8601_FORMAT);
        Date dt  = new Date();
        dt.setTime(value);
        return df1.format(dt);
    }
}
资源法

     @GET
     @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
     @Path("/epochtime")
     public Long getEpochTime(@DateEpochMarker @QueryParam("startTime") Long          startEpochTime){
    return startEpochTime;
}

我觉得回答自己的问题很愚蠢。
不知何故,我无法使上述功能正常工作,最终使用的是自定义Jodatime转换器。
粘贴代码,以便遇到相同查询的任何人都可以得到答案

@Provider
public class DateTimeParamConverterProvider implements ParamConverterProvider {

    private static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(DateTimeParamConverterProvider.class);

    @Override
    public <T> ParamConverter<T> getConverter(Class<T> type, Type genericType, Annotation[] annotations) {
        if (type.equals(DateTime.class)) {
            return (ParamConverter<T>) new DateTimeParamConverter();
        } else {
            return null;
        }

    }

    private static class DateTimeParamConverter implements ParamConverter<DateTime> {
        @Override
        public DateTime fromString(String value) {
            LOGGER.debug("The ISO Date that is provided is   {}", value);
            try {
                return ISODateTimeFormat.dateTimeNoMillis().parseDateTime(value);
            } catch (IllegalArgumentException e) {
                return ISODateTimeFormat.dateTime().parseDateTime(value);
            }
        }

        @Override
        public String toString(DateTime value) {
            return value.toString();
        }

    }
}
@Provider
公共类DateTimeParamConverterProvider实现ParamConverterProvider{
私有静态最终记录器Logger=org.slf4j.LoggerFactory.getLogger(DateTimeParamConverterProvider.class);
@凌驾
public ParamConverter getConverter(类类型、类型genericType、注释[]注释){
if(type.equals(DateTime.class)){
返回(ParamConverter)新的DateTimeParamConverter();
}否则{
返回null;
}
}
私有静态类DateTimeParamConverter实现ParamConverter{
@凌驾
public DateTime fromString(字符串值){
debug(“提供的ISO日期为{}”,值);
试一试{
返回ISODateTimeFormat.dateTimeNoMillis().parseDateTime(值);
}捕获(IllegalArgumentException e){
返回ISODateTimeFormat.dateTime().parseDateTime(值);
}
}
@凌驾
公共字符串toString(日期时间值){
返回值.toString();
}
}
}
;只是要确保以后接受它(尽管你,因为48小时的等待时间),
@Provider
public class DateTimeParamConverterProvider implements ParamConverterProvider {

    private static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(DateTimeParamConverterProvider.class);

    @Override
    public <T> ParamConverter<T> getConverter(Class<T> type, Type genericType, Annotation[] annotations) {
        if (type.equals(DateTime.class)) {
            return (ParamConverter<T>) new DateTimeParamConverter();
        } else {
            return null;
        }

    }

    private static class DateTimeParamConverter implements ParamConverter<DateTime> {
        @Override
        public DateTime fromString(String value) {
            LOGGER.debug("The ISO Date that is provided is   {}", value);
            try {
                return ISODateTimeFormat.dateTimeNoMillis().parseDateTime(value);
            } catch (IllegalArgumentException e) {
                return ISODateTimeFormat.dateTime().parseDateTime(value);
            }
        }

        @Override
        public String toString(DateTime value) {
            return value.toString();
        }

    }
}