我正在尝试在 dynamoDb 表中添加时间属性。我在我的日期容器上添加了 @DynamoDBAutoGeneratedTimestamp 注释,但它似乎选择 00:00 作为默认时区。
@get:DynamoDBAutoGeneratedTimestamp(strategy=DynamoDBAutoGenerateStrategy.CREATE)
var createdAt: String? = null
@get:DynamoDBAutoGeneratedTimestamp(strategy=DynamoDBAutoGenerateStrategy.ALWAYS)
var updateAt: String? = null
最佳答案
无法为 @DynamoDBAutoGeneratedTimestamp
设置区域偏移量,但可以创建您自己的 @DynamoDBAutoGenerator 实现连同 corresponding annotation .
下面是您将如何在 Java 中完成它。 (看起来您使用的是 Kotlin,但转换它应该很简单。)
@DynamoDBAutoGenerated(generator=AutoGeneratedTimestampWithOffset.Generator.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface AutoGeneratedTimestampWithOffset {
/**
* See {@link ZoneOffset#of(String)} for valid values.
*/
String offset();
DynamoDBAutoGenerateStrategy strategy() default DynamoDBAutoGenerateStrategy.ALWAYS;
public class Generator implements DynamoDBAutoGenerator<String> {
private final String offset;
private final DynamoDBAutoGenerateStrategy strategy;
public Generator(final Class<String> targetType, final AutoGeneratedTimestampWithOffset annotation) {
this.offset = annotation.offset();
this.strategy = annotation.strategy();
}
@Override
public DynamoDBAutoGenerateStrategy getGenerateStrategy() {
return strategy;
}
@Override
public final String generate(final String currentValue) {
return OffsetDateTime.ofInstant(Instant.now(), ZoneOffset.of(offset)).toString();
}
}
}
在您的 @DynamoDBTable
类中,您可以像这样使用此注释:
@get:AutoGeneratedTimestampWithOffset(offset="+05:30", strategy=DynamoDBAutoGenerateStrategy.CREATE)
var createdAt: String? = null
@get:AutoGeneratedTimestampWithOffset(offset="+05:30", strategy=DynamoDBAutoGenerateStrategy.ALWAYS)
var updateAt: String? = null
关于java - 如何为 DynamoDB 设置时区在 spring 数据中自动生成时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57072884/