Skip to content
Advertisement

How to use AWSDateTime in Java

Trying to figure out how to input a AWSDateTime. I don’t want to use Java 8.

createDate requires Temporal.DateTime

com.amplifyframework.core.model.temporal
public static final class Temporal.DateTime

private final @ModelField(targetType="AWSDateTime", isRequired = true) Temporal.DateTime createDate;

Is there any other way I can accomplish this?

Date input = new Date();
LocalDate date = input.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); --> this requires Java 8

Todo todo = Todo .builder()
        .createDate(date)
        .build();

Amplify.DataStore.save(todo,
        result -> Log.i("MyAmplifyApp", "Created successfully"),
        error -> Log.e("MyAmplifyApp",  "Error creating", error)

Advertisement

Answer

I ended up using the function in DateUtils.

https://docs.aws.amazon.com/appsync/latest/devguide/scalars.html

AWSDateTime

An extended ISO 8601 date and time string in the format YYYY-MM-DDThh:mm:ss.sssZ.

String date1 = com.amazonaws.util.DateUtils.formatISO8601Date(new Date());

Todo todo = Todo .builder()
        .createDate(new Temporal.DateTime(date1))
        .build();

Note: This will store the date in GMT time.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement