DynamoDBMapper for java.time.LocalDateTime
Despite what I said I found it simple enough to use DynamoDBMarshalling
to marshal to and from a string. Here is my code snippet and an AWS reference:
class MyClass {
...
@DynamoDBMarshalling(marshallerClass = LocalDateTimeConverter.class)
public LocalDateTime getStartTime() {
return startTime;
}
...
static public class LocalDateTimeConverter implements DynamoDBMarshaller<LocalDateTime> {
@Override
public String marshall(LocalDateTime time) {
return time.toString();
}
@Override
public LocalDateTime unmarshall(Class<LocalDateTime> dimensionType, String stringValue) {
return LocalDateTime.parse(stringValue);
}
}
}
No AWS DynamoDB Java SDK can't map java.time.LocalDateTime natively without using any annotation.
To do this mapping, you have to use DynamoDBTypeConverted
annotation introduced in the version 1.11.20 of the AWS Java SDK. Since this version, the annotation DynamoDBMarshalling
is deprecated.
You can do that like this:
class MyClass {
...
@DynamoDBTypeConverted( converter = LocalDateTimeConverter.class )
public LocalDateTime getStartTime() {
return startTime;
}
...
static public class LocalDateTimeConverter implements DynamoDBTypeConverter<String, LocalDateTime> {
@Override
public String convert( final LocalDateTime time ) {
return time.toString();
}
@Override
public LocalDateTime unconvert( final String stringValue ) {
return LocalDateTime.parse(stringValue);
}
}
}
With this code, the stored dates are saved as string in the ISO-8601 format like that: 2016-10-20T16:26:47.299
.