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.
ISO8601DateFormat
and ISO8601Utils
are deprecated since version 2.9
and do not provide clear documentation what to do after it was deprecated. There was a question on GitHub: ISO8601DateFormat is deprecated but replacement is unclear related to your question.
I guess, that someone from your team just used first class which he/she found to serialise Date
to ISO8601
format and did not get to big attention that it comes from Jackson
library. I recommend do not use internal *Utils
classes from libraries you use for other reason. In that case for serialising
/deserialising
JSON
. Right now, we have many options how to format dates and you should pick one for you. Mostly, it depends which JDK
version do you use.
So, instead:
String value = ISO8601Utils.format(date, true);
Use for example SimpleDateFormat
:
DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
As you can see below example prints the same date for both cases:
import com.fasterxml.jackson.databind.util.ISO8601Utils;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class JsonApp {
public static void main(String[] args) throws Exception {
Date date = new Date();
DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
df2.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(ISO8601Utils.format(date, true));
System.out.println(df2.format(date));
}
}
Above code prints:
2019-02-19T19:37:14.809Z
2019-02-19T19:37:14.809Z
Second parameter to ISO8601Utils.format
is true
which means that you need milliseconds in result and format should look like yyyy-MM-ddThh:mm:ss.sss'Z'
.
For more info, read: