Query Date Range in DynamoDB Using Java (Guide w/ Code Examples)

Provided by Rafal Wilinski
Last updated: October 23rd, 2026
Store fixed-width UTC timestamps in the sort key, then query one partition with BETWEEN. The endpoints are inclusive. The paginator reads every page, including ranges larger than a single response.
This example uses Java 17+ and the software.amazon.awssdk:dynamodb SDK 2.x dependency. Configure an AWS Region and credentials through your usual AWS profile or runtime role. The Orders table has String keys pk and sk; adapt the table and keys to your schema.
import java.util.*; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.*; public class Example { static AttributeValue s(String value) { return AttributeValue.builder().s(value).build(); } static Map<String, AttributeValue> key(String order) { return Map.of("pk", s("CUSTOMER#123"), "sk", s(order)); } public static void main(String[] args) throws InterruptedException { try (DynamoDbClient ddb = DynamoDbClient.create()) { QueryRequest request = QueryRequest.builder().tableName("Orders") .keyConditionExpression("#pk = :pk AND #sk BETWEEN :start AND :end") .expressionAttributeNames(Map.of("#pk", "pk", "#sk", "sk")) .expressionAttributeValues(Map.of(":pk", s("CUSTOMER#123"), ":start", s("2026-10-01T00:00:00.000Z"), ":end", s("2026-10-31T23:59:59.999Z"))).build(); for (QueryResponse page : ddb.queryPaginator(request)) { page.items().forEach(System.out::println); } } } }
Use the same timestamp precision and time zone for every stored sort key. If timestamps are non-key attributes, create an index for this access pattern.
Reference: AWS Query API. For related access patterns, see DynamoDB query examples.
Similar Code Examples
- Remove with DynamoDB Mapper
- Conditional Update in DynamoDB Using Java
- Batch Delete Using DynamoDB Mapper
- Query Hash Key Only in DynamoDB Using Java
- Remove Item in DynamoDB Using Java
- Batch Write Using DynamoDB Mapper
- Batch Update Using DynamoDB Mapper
- Get Item Request in DynamoDB Using Java
- Get Multiple Items in DynamoDB Using Java
- Remove Attribute in DynamoDB Using Java
- Query Index in DynamoDB Using Java
- Update Expression in DynamoDB Using Java
- Delete Multiple Items in DynamoDB Using Java
- Insert in DynamoDB Using Java
- Get All Items from DynamoDB Using Java
Dynobase is a Professional GUI Client for DynamoDB
Start your 7-day free trial today
Product Features
DynamoDB Tools
- SSH GUI Client
- Chat With Cloud
- MongoDB GUI Client
- AWS Cost Monitoring
- DynamoDB Query Builder
- DynamoDB Code Examples
- DynamoDB Node.js Queries
- DynamoDB JSON Converter
- DynamoDB UUID Generator
- DynamoDB Local Admin GUI
- DynamoDB Pricing Calculator
- DynamoDB Single-Table Design
- Best DynamoDB GUI Clients
- Dynobase vs DynoTable