簡體   English   中英

映射列表<localdatetime>到 DynamoDB</localdatetime>

[英]Mapping List<LocalDateTime> to DynamoDB

我正在嘗試 map 到 Java 中的 DynamoDB 日期列表

@DynamoDBTypeConverted(converter = LocalDateTimeConverter.class)
private List<LocalDateTime> acquisitionsDates;



public class LocalDateTimeConverter implements DynamoDBTypeConverter<String, LocalDateTime> {

    @Override
    public String convert(LocalDateTime dateTime) {
        return dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
    }

    @Override
    public LocalDateTime unconvert(String dateTimeStr) {
        return LocalDateTime.parse(dateTimeStr);
    }
}

我已經編寫了自己的轉換器,但它僅適用於 LocalDateTime 而不適用於列表。 有誰知道如何正確地做到這一點?

我是否應該編寫單獨的轉換器來返回字符串列表,其中每個字符串都將從本地日期時間轉換?

在接口 DynamoDBTypeConverter<S,T> 中表示 S - DynamoDB 標准類型,T - 對象的字段/屬性類型。 使用 T 作為列表。

public class LocalDateTimeConverter implements DynamoDBTypeConverter<String, List<LocalDateTime>> {

    @Override
    public String convert(List<LocalDateTime> dateTime) {
        //your implementation
    }

    @Override
    public List<LocalDateTime> unconvert(String dateTimeStr) {
        //your implementation
    }
}

我寫了下面這樣的轉換器,它可以按我的意願工作;)

public class ListOfLocalDateTimesConverter implements DynamoDBTypeConverter<List<String>, List<LocalDateTime>> {
@Override
public List<String> convert(List<LocalDateTime> localDateTimes) {
    return localDateTimes
            .stream()
            .map(localDateTime -> localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))
            .collect(Collectors.toList());
}

@Override
public List<LocalDateTime> unconvert(List<String> strings) {
    return strings
            .stream()
            .map(str -> LocalDateTime.parse(str))
            .collect(Collectors.toList());
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM