0

my service take a json body and saves it into db my json body looks like : { "id": "1059", "firstName": "max", "lastName": "sam", "emailAddress": "abc@gmail.com", "joiningDate": "2018-07-02T09:55:24.600", "endingDate": "2018-07-02T04:55:24.677" }

in my case date format is "yyyy-MM-dd'T'HH:mm:ss.SSS" and should not allow 'z' at the end of the date. but when i stored it into cassandra the field(timestamp) has the value "2018-07-02 09:55:24.600Z"

i am taking json date fields as string in the pojo and parsing as date then validating them and my validation looks like

public class DateValidator  {

    @Override
    public boolean isValid(String field) {

        if (field == null) {
            return ignoreNull ? true : false;
        }

        boolean isValid = true;
        try {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
            LocalDateTime.parse(field, formatter);
        } catch (Exception exp) {
            isValid = false;
        }
        return isValid;
    }
sunny
  • 33
  • 5

1 Answers1

1

Even if you don't specify a timezone, when the timestamp is stored into Cassandra, the timezone will be added.

If no time zone is specified, the time zone of the Cassandra coordinator node handling the write request is used.

The recommendation is to specify the timezone and not rely on the timezone specified on the Cassandra nodes.

If you need to display the timestamp in cqlsh without the timezone you could change your cqlshrc file (the default format for cqlsh is yyyy-mm-dd HH:mm:ssZ)

[ui]
          datetimeformat = %Y-%m-%d %H:%M

In java you could easily remove the timezone or you could just store the timestamp as string if timezone is an issue.

Some stuff related to this:1, 2, 3.

Horia
  • 2,942
  • 7
  • 14