4

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.

lovprogramming
  • 593
  • 11
  • 24
  • Your question seems to be missing the what you're trying to do and how your trying to do it and jumped into a how do I do this method when we don't know what you're trying to do with it. you would be better off explaining what it is your trying to do. – Barkermn01 Feb 19 '19 at 17:34
  • Thanks for the response! I am just trying to replace the deprecated ISO8610Utils class with the newly replaced one. I was told to use external json schema generator module to replace the usage of ISO8601Utils. I read through the link I provided, but I don't understand how that would replace the line the SonarQube is throwing the warning at, which is `String value = ISO8601Utils.format(date, true);`. I want to replace this line with a new line so that SonarQube is not throwing anymore error on it and the code compiles okay. Ultimately, I'm just trying to clean up the SonarQube error right now. – lovprogramming Feb 19 '19 at 17:40
  • 1
    But that makes no sense JSON Schema is a method of defining a JSON object structure ideal for verification of transferred data sort of like SOAP for XML. it appears it's just trying to date format if that is the case why don't you use the default `java.util.Date` https://stackoverflow.com/questions/18480633/java-util-date-format-conversion-yyyy-mm-dd-to-mm-dd-yyyy/18480709 – Barkermn01 Feb 19 '19 at 17:43

1 Answers1

6

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:

  1. Converting ISO 8601-compliant String to java.util.Date
  2. How to convert time in YYYY-MM-DDTHH:mm:ss.SSSZ format to default timezone?
Michał Ziober
  • 37,175
  • 18
  • 99
  • 146