Multiple DateTime formats in Movies

I’m confused - there are multiple formats of date in movies therefore the transformDates method will by necessity fail on some dates since you only have the method look for one format. i.e. If I have the date format set to"yyyy-MM-dd HH:mm:ss.SSSSSSSSS" so as to read the most common date format of in movies (eg. -> ‘2015-03-22 02:35:41.983000000’), the line that converts the string to date will fail on anything else.
The transform date code reads

try {
if (lastUpdated != null) {
return new UpdateOneModel<>(
Filters.eq("_id", doc.getObjectId("_id")),
***** Updates.set(“lastupdated”, dateFormat.parse(lastUpdated)));
}
} catch (ParseException e) {
System.out.println(
MessageFormat.format(
"String date {0} cannot be parsed using {1} " + “format: {2}”,
lastUpdated, dateFormat, e.getMessage()));
}

All that the ***** line is doing is reading in the string according to specific format. If the format differs then that line raises an exception of
String date 2015-09-12 00:38:27 cannot be parsed using java.text.SimpleDateFormat@ad915405 format: Unparseable date: “2015-09-12 00:38:27”
NOTE the difference between
“2015-09-12 00:38:27” and
“2015-03-22 02:35:41.983000000”

The term ISODate is confusing as well. Are you referring to a specific format? like ISO 8601? 8601 is defined as something completely different from the format of the date strings in movies. There is an ISODate wrapper described in some of the Mongo db docs (https://docs.mongodb.com/manual/reference/method/Date/) but nothing in the Java Driver and no code completion within intellij for ‘ISODate’ (except for a deprecated jackson binding).
Are we supposed to change transformDates (there are no comments indicating to do so) to handle the different date formats that have to be read in and determine the right ISO format to write them out again?
An alternative is to run Migrator with several (at least 2) date formats until there are no more exceptions being raised.
Another alternative is to have a truncated date format that does not include sub-seconds, even though most current date/times string include those.
This last alternative is what I did and the tests pass … but … The ticket fails to pass
“Migration: It does not appear that you correctly converted the type”

What should the dates look like in the end?

1 Like

Hi @Cameron_55889,

In the DateTime format, it is currently as string data type, after the conversion, it should be datetime.

For learning more about Date object in MongoDB, here are some great documentation links:

https://docs.mongodb.com/manual/reference/method/Date/index.html

https://docs.mongodb.com/manual/reference/bson-types/#document-bson-type-date

Kanika