I am replacing ISO8601Utils
which is commented below due to SonarQube throwing the following error: Remove this use of "ISO8601Utils"; it is deprecated.
To replace it, I would use the external json schema generator module, https://github.com/FasterXML/jackson-module-jsonSchema or something else. I read through the link, but don't understand how to use object mapper to turn to replace this line: 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();
}
}
Any help would be greatly appreciated!
P.S. I am writing a unit test to verify that both the ISO8601Utils and SimpleDateFormat has the same format.
My updated class:
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();
}
}
My test method:
@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));
}
However, I am getting null pointer exception for the commented line. I assume it has to do with injecting or mocking object, but I am not sure how I should approach this problem.