Using Joda-Time to generate the correct ISODate to insert Mongo

I am trying to update date fields in mongo that require the ISODate format. In mango, it looks like this:

"crDt" : ISODate("2013-08-19T17:21:57.549Z") 

I use the Java structure that I use to limit the use of strings as test parameters, so I am trying to use this string using DateTimeFormatter to get it in the correct ISODateTimeFormat and then pass this to mongo. I can’t just pass in a string similar to what I have above. Trying to do this screw the field in mongo. The corresponding bits of the Joda-Time code that I use is as follows:

 //I can't get this right. String crDt = "2013-01-19T15:28:58.851Z"; DateTimeFormatter parser = ISODateTimeFormat.dateHourMinuteSecondMillis(); parser.parseDateTime(crDt); // this method updates the record in mongo. This method totally works, so no // point in pasting it here, I just can't get the parser object correct to be // in the correct format once inserted, it needs to be the correct ISODate form. mongo.setCrDt(recordId, parser); 

And when the code runs, I get such errors from the .parseDateTime method:

 java.lang.IllegalArgumentException: Invalid format: "2013-01-19T15:28:58.851Z" is malformed at "Z" at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:866) 

I can say that the line I am giving is incorrect in order to get into the details. I tried to get away with Z , I tried other combos, but every time he says that he is distorted. So what is my starting line to make .parseDateTime work and give me an object that looks right?

EDIT:

Updated to try the suggestions below. The problem I'm facing right now is an IllegalArgumentException, cannot serialize the org.joda.time.DateTime class. So it seems that the objects remaining in the living objects of the Jedi are missing? I also looked at another suggestion, looking at wireframes like Spring Data. It seems that there is much more that you need to delve into. Is there really no easy way to keep this in a mango?

EDIT2:

OK, I think I have it now. I might not have a full understanding of all the mechanics in the game, but BasicDBObject will not play well with DateTime . Date objects seem to be the only way, at least in the implementation I'm dealing with. I have done the following:

 DateTimeFormatter parser = ISODateTimeFormat.dateTime(); DateTime result; Date newResult; result = parser.parseDateTime(crDt); newResult = result.toDate(); 

Then I passed newResult to the BasicDBObject object, then to update the entry in mongo. It works great and the recording is updated correctly.

+6
source share
3 answers

The format of your input string is correct, as it is intended to indicate UTC.

Modify your parser to use one that matches this format:

 DateTimeFormatter parser = ISODateTimeFormat.dateTime(); 

The rest of your question doesn't make much sense to me. You should not pass a parser , but rather a return value from parseDateTime , which you do not seem to capture.

 DateTime result = parser.parseDateTime(crDt); mongo.setCrDt(recordId, result.toDate()); 

Whether this last line will work depends on what this function accepts.

+7
source

I solved this by adding an "Encoding Hook" in the constructor of the Service class, where I update MongoDB. This will allow you to use org.joda.time.DateTime in your code, and it will be saved as java.util.Date in MongoDB.

MyService.java

 @Inject public MyService(com.mongodb.Client client) { BSON.addEncodingHook(DateTime.class, new JodaTimeTransformer()); BSON.addDecodingHook(Date.class, new JodaTimeTransformer()); this.mongoClient = mongoClient; } 

JodaTimeTransformer.java

 import java.util.Date; import org.joda.time.DateTime; public class JodaTimeTransformer implements org.bson.Transformer { @Override public Object transform(Object o) { if(o instanceof DateTime) { return ((DateTime)o).toDate(); } else if(o instanceof Date) { return new DateTime((Date) o); } throw new IllegalArgumentException("JodaTimeTransformer can only be used with DateTime or Date"); } } 
+5
source

Matt Johnson's answer is correct. But it can be even simpler: pass ( ISO 8601 ) the string directly to the DateTime constructor. No formatting needed.

Pay attention to the time zone. The DateTime object in Joda-Time really knows its own time zone, unlike the java.util.Date object. Do you want your DateTime object to be assigned a default JVM time zone, not a time zone (UTC), or a specific time zone?

For the date assigned by default time zone.

 DateTime dateTime = new DateTime( "2013-01-19T15:28:58.851Z" ); 

For the date set by UTC / GMT (without a time zone offset).

 DateTime dateTime = new DateTime( "2013-01-19T15:28:58.851Z", DateTimeZone.UTC ); 

For the date assigned to a specific time zone.

 DateTime dateTime = new DateTime( "2013-01-19T15:28:58.851Z", DateTimeZone.forId( "Europe/Paris" ) ); 
+3
source

Source: https://habr.com/ru/post/952056/


All Articles