n00b n00b - 1 month ago 29
Java Question

DynamoDBMapper for java.time.LocalDateTime

I'm utilizing java.time.LocalDateTime in my java application. I'm also trying to use

DynamoDBMapper
and via the annotation save the
LocalDateTime
variable. Unfortunately I get the following error:

DynamoDBMappingException: Unsupported type: class java.time.LocalDateTime


Is there a way to have this mapping without using
DynamoDBMarshalling
?

Answer

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);
        }
    }
}