【发布时间】:2019-02-19 17:28:50
【问题描述】:
我正在替换 ISO8601Utils,由于 SonarQube 引发以下错误,下面对此进行了评论:Remove this use of "ISO8601Utils"; it is deprecated. 要替换它,我将使用外部 json 模式生成器模块 https://github.com/FasterXML/jackson-module-jsonSchema 或其他东西。我通读了链接,但不明白如何使用对象映射器来替换这一行:String value = ISO8601Utils.format(date, true);
public static class ISO8601DateFormat extends DateFormat {
public ISO8601DateFormat() {}
public StringBuffer format(Date date, StringBuffer toAppendTo,
FieldPosition fieldPosition) {
String value = ISO8601Utils.format(date, true);
//Im not sure how I can replace this line with a new
//replacement
toAppendTo.append(value);
return toAppendTo;
}
public Date parse(String source, ParsePosition pos) {
pos.setIndex(source.length());
return ISODateTimeFormat.dateTimeParser().parseDateTime(source).toDate();
}
public Object clone() {
return this;
}
public String toString() {
return this.getClass().getName();
}
}
任何帮助将不胜感激!
附:我正在编写一个单元测试来验证 ISO8601Utils 和 SimpleDateFormat 是否具有相同的格式。
我更新的课程:
public static class ISO8601DateFormat extends DateFormat {
public static final long serialVersionUID = 3549786448500970210L;
public ISO8601DateFormat() {}
@Override
public StringBuffer format(Date date, StringBuffer toAppendTo,
FieldPosition fieldPosition) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
String value = dateFormat.format(date);
toAppendTo.append(value);
return toAppendTo;
}
public Date parse(String source, ParsePosition pos) {
pos.setIndex(source.length());
return ISODateTimeFormat.dateTimeParser().parseDateTime(source).toDate();
}
public Object clone() {
return this;
}
public String toString() {
return this.getClass().getName();
}
}
我的测试方法:
@Test
public void testDateFormat() {
df = new DefaultHttpClientUtil.ISO8601DateFormat();
Date date = new Date();
// df.setTimeZone(TimeZone.getTimeZone("GMT")); I'm getting NPE
// for this line
assertThat(df.format(date)).isEqualTo(ISO8601Utils.format(date,
true));
}
但是,我收到注释行的空指针异常。我认为它与注入或模拟对象有关,但我不确定应该如何解决这个问题。
【问题讨论】:
-
您的问题似乎错过了您正在尝试做什么以及您如何尝试这样做,并跳入了当我们不知道您在尝试什么时我该如何执行此方法与它有关。你最好解释一下你想做什么。
-
感谢您的回复!我只是想用新替换的类替换已弃用的 ISO8610Utils 类。有人告诉我使用外部 json 模式生成器模块来代替 ISO8601Utils 的使用。我通读了我提供的链接,但我不明白这将如何替换 SonarQube 发出警告的行,即
String value = ISO8601Utils.format(date, true);。我想用新行替换这一行,这样 SonarQube 就不会再抛出错误并且代码编译正常。最终,我现在只是想清理 SonarQube 错误。 -
但这没有任何意义 JSON Schema 是一种定义 JSON 对象结构的方法,非常适合于验证传输的数据,类似于 XML 的 SOAP。如果是这种情况,它似乎只是在尝试日期格式,为什么不使用默认的
java.util.Datestackoverflow.com/questions/18480633/…
标签: java json jackson sonarqube deprecated