How to get Last Modified Date of a Confleunce Space programmatically?

I am trying to figure out the new way of retrieving a Confluence Space programmatically in Java. Currently I have achieved this in code, which does return the correct space object:

@Path("/getunusedspaces")
    @GET
    @AnonymousAllowed
    @Consumes
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Response getUnusedSpaces(@QueryParam("date") String date, @Context UriInfo uriInfo) {
        log.info("Data Received: " + uriInfo.getRequestUri().getQuery());
        try {
            Optional<Space> spaceJTS = spaceService.find(new Expansion("history.lastupdated")).withKeys("JTS").fetch();
            JSONObject obj = new JSONObject();
            obj.put("auth", true);
            obj.put("spaceKey", spaceJTS.get().getKey());
            log.error("Space details: " + spaceJTS.toString());
            String ret = obj.toString();
            return Response.status(Response.Status.OK).entity(ret).type(MediaType.TEXT_PLAIN).build();
        } catch (JSONException e) {
            return Response.status(Response.Status.NOT_FOUND).entity("Could not found space object").type(MediaType.TEXT_PLAIN).build();
        }
    }

However, how do I get that “history.lastupdated” value? I have tried various ways but I can’t seem to retrieve it. Any ideas?

There are actually two dates.

  1. Last time the space was updated
  2. Last time content in the space was updated

I doubt you want the space’s last updated date.
You will have to find some way of getting the most recently updated content’s date.

What I’ve done is something like this:

lastModList = contentEntityManager.getRecentlyModifiedEntities(spaceKey, 0)
lastUpdated = lastModList[0].getLastModificationDate()

This is leaps and bounds better than looping through all the content in a space. It does include all content (pages, attachments, blog posts, etc.), so if you’re looking for a specific type, you’d need to do something else.

btw I don’t know why Atlassian doesn’t provide a date like this directly since it’s a really common ask