'How to scan a table in DynamoDB with time period?

I have a table in DynamoDB and it has an attribute 'createDate' and I want to do a scan using a filter in a specific period of that attribute (for example: 2022-01-01 to 2022-01-31) but I don't know exactly if it's possible and how to do. If anyone has done this and can help me it would be very helpful.

just one more question: is it possible to put the result in a CSV file?

Here is my code where I can scan with a single date:

public class QueryTableResearchAnswers {

    static AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
    static DynamoDB dynamoDB = new DynamoDB(client);

    static String tableName = "research-answers";

    public static void main(String[] args) throws Exception {

        String researchAnswers = "Amazon DynamoDB";

        findAnswersWithinTimePeriod(researchAnswers);
        //findRepliesPostedWithinTimePeriod(researchAnswers);
    }

    private static void findAnswersWithinTimePeriod(String researchAnswers) {
        Table table = dynamoDB.getTable(tableName);

        Map<String, Object> expressionAttributeValues = new HashMap<String, Object>();
        expressionAttributeValues.put(":startDate", "2022-01-01T00:00:00.0Z" );

        
        ItemCollection<ScanOutcome> items = table.scan("createDate between > startDate", // FilterExpression
                "bizId, accountingsessionid, accounttype, acctsessionid, choicecode, contextname, createDate, document, framedipaddress," +
                  "macaddress, macaddressnetworkdata, machash, mail, nasgrelocalip, nasidentifier, nasipaddress, nasportid, network, networktype, networkuuid, phone," +
                "question, questionanswer, questioncode, realm, relayingmacaddress, remoteipaddress, useragent, username", // ProjectionExpression
                null, // ExpressionAttributeNames - not used in this example
                expressionAttributeValues);

        System.out.println("Scan of " + tableName + " for january answers");
        Iterator<Item> iterator = items.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next().toJSONPretty());
        }
    }


Solution 1:[1]

In general, for an arbitrary date range:

createDate BETWEEN :date1 AND :date2

But, in your specific case of 2022-01-01 to 2022-01-31 (the entire month of January), you can simplify this to:

beginsWith(createDate, "2022-01")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jarmod