如何为 DynamoDB 设置时区在 spring 数据中自动生成时间戳

我正在尝试在 dynamoDb 表中添加时间属性。我在我的日期容器上添加了 @DynamoDBAutoGeneratedTimestamp 注释,但它似乎选择 00:00 作为默认时区。


@get:DynamoDBAutoGeneratedTimestamp(strategy=DynamoDBAutoGenerateStrategy.CREATE)

    var createdAt: String? = null


    @get:DynamoDBAutoGeneratedTimestamp(strategy=DynamoDBAutoGenerateStrategy.ALWAYS)

    var updateAt: String? = null


三国纷争
浏览 134回答 1
1回答

阿波罗的战车

无法为 设置区域偏移量@DynamoDBAutoGeneratedTimestamp,但可以创建您自己的@DynamoDBAutoGenerator实现以及相应的注释。以下是您将如何在 Java 中完成它。(看起来您使用的是 Kotlin,但转换它应该很简单。)@DynamoDBAutoGenerated(generator=AutoGeneratedTimestampWithOffset.Generator.class)@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.METHOD})public @interface AutoGeneratedTimestampWithOffset {&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* See {@link ZoneOffset#of(String)} for valid values.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; String offset();&nbsp; &nbsp; DynamoDBAutoGenerateStrategy strategy() default DynamoDBAutoGenerateStrategy.ALWAYS;&nbsp; &nbsp; public class Generator implements DynamoDBAutoGenerator<String> {&nbsp; &nbsp; &nbsp; &nbsp; private final String offset;&nbsp; &nbsp; &nbsp; &nbsp; private final DynamoDBAutoGenerateStrategy strategy;&nbsp; &nbsp; &nbsp; &nbsp; public Generator(final Class<String> targetType, final AutoGeneratedTimestampWithOffset annotation) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.offset = annotation.offset();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.strategy = annotation.strategy();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public DynamoDBAutoGenerateStrategy getGenerateStrategy() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return strategy;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public final String generate(final String currentValue) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return OffsetDateTime.ofInstant(Instant.now(), ZoneOffset.of(offset)).toString();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}在你的@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
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java