Here is the issue I'm having. In my kotlin code (in an Android project), I have something like this:
val rDate = Util.formatDateAsIso8601(Date())
The formatDateAsIso8601(Date inputDate)
is a Java method and looks like this:
public static String formatDateAsIso8601(final Date inputDate) {
TimeZone tz = TimeZone.getDefault();
mDateFormat.setTimeZone(tz);
return mDateFormat.format(inputDate);
}
where
mDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.ENGLISH);
Once I get rDate
, it populates an Object field and then serialized to json. The issue is that the value for the date is really weird. The first issue I saw was getting a date of 2018-11-31, which is an invalid date. Now I'm seeing values with extra 0's, such as '2018-11-007T20:09:26.533-0500' and '2018-0011-007T020:18:00.367-0500' and even '2018-0011-007T020:27:22.712-0500'. This issue is happening sporadically. Most of the time the dates are fine, but sometimes there are quite a few instances of this stuff happening, and it really only started happening fairly relatively recently.
Any ideas what is going on here?
--Edit-- I now create the SimpleDateFormat object with every call, but I am still getting these weird date values. Here is the new format method:
public static String formatDateAsIso8601(final Date inputDate) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.ENGLISH);
TimeZone tz = TimeZone.getDefault();
dateFormat.setTimeZone(tz);
return dateFormat.format(inputDate);
}