Skip to main content
Question

Java SDK - get created at for a file seems not to work

  • May 22, 2025
  • 3 replies
  • 53 views

Forum|alt.badge.img

Hey,

 

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.

BOX_9x6yrpl4kbaxrh1otzxwoeq63c1iiwcm.png

Is there another (correct) way to get the created at value from the file info, or is this a bug?

 

Cheers,

Felix

3 replies

Forum|alt.badge.img

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:

  1. make a new BoxFile object using the ID of the BoxFile.Info
  2. from this new BoxFile object get again the BoxFile.Info
  3. 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));
    }
}

 

 


Forum|alt.badge.img

 Thanks for updating the thread with the solution!


Forum|alt.badge.img

String[] childrenFields = {"name", "createdAt", "size"};

Iterable<BoxItem.Info> infos = folder.getChildren(childrenFields);

 

"size" gets populated but not "createdAt". Better to get a new BoxFile.Info object from the id

to fetch the "createdAt"