Java SDK - get created at for a file seems not to work
AnsweredHey,
I'm trying to get the last created file in a folder using the Java SDK.
I iterate over each file in that folder and then use the getCreatedAt() method to compare which is the last file. It seems that the value I get from the method is wrong. Or I'm not using the right method.
Here is a simplified snippet of my code.
// here I just connect to my Box BoxDeveloperEditionAPIConnection api = connectToTheBox(); BoxFolder folder = new BoxFolder(api, "***number removed for privacy***"); System.out.println("Iterating over the folder: " + folder.getInfo().getName());
for (BoxItem.Info itemInfo : folder) { if (itemInfo instanceof BoxFile.Info) { BoxFile.Info fileInfo = (BoxFile.Info) itemInfo; System.out.println(" File: " + fileInfo.getName());
DateTime curCreatedAt = new DateTime(fileInfo.getCreatedAt()); DateTimeFormatter dateTimeFormat = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"); System.out.println(" created_at: " + dateTimeFormat.print(curCreatedAt)); } }
Output is the following:
Iterating over the folder: CallData File: BI Report KW 49.xlsx created_at: 2017-12-07 17:31:25 File: BI Report.xlsx created_at: 2017-12-07 17:31:25
Here is a screenshot from the Box and my Java Console where I highlighted the points.
Is there another (correct) way to get the created at value from the file info, or is this a bug?
Cheers,
Felix
-
Hey,
I found out what is the error.
First I was using a date time library function what returns the current time when the object is null. So the time I printed on the console actually was null.
So I searched the community with this new insight and I found this post, which is exactly my problem:
BoxItem.Info returns null for various properties
So you can't get the created at from the item BoxFile.Info directly. Here is how it works:
- make a new BoxFile object using the ID of the BoxFile.Info
- from this new BoxFile object get again the BoxFile.Info
- on this BoxFile.Info object you can now get the created at time stamp
So it is not a bug, it is rather on misleading design.
Here is the changed code
for (BoxItem.Info itemInfo : folder) { if (itemInfo instanceof BoxFile.Info) { BoxFile.Info fileInfo = (BoxFile.Info) itemInfo; System.out.println(" File: " + fileInfo.getName()); // new part - first get the BoxFile and then again the BoxFile.Info of that BoxFile boxFile = new BoxFile(api, fileInfo.getID()); BoxFile.Info newBoxFileInfo = boxFile.getInfo(); DateTime curCreatedAt = new DateTime(newBoxFileInfo.getCreatedAt()); DateTimeFormatter dateTimeFormat = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"); System.out.println(" created_at: " + dateTimeFormat.print(curCreatedAt)); } }
Please sign in to leave a comment.
Comments
3 comments