2

I am trying to enter timestamp using DateTimeField but in my entity i am having java.sql.timestamp. Converting datetimefield to timestamp is giving error

ConversionClass

package com.vaadin.convertor;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import com.vaadin.data.Converter;
import com.vaadin.data.Result;
import com.vaadin.data.ValueContext;
@SuppressWarnings("serial")
public class StringTimestampConvertor implements Converter<LocalDateTime, 
Timestamp> {

@SuppressWarnings("unchecked")

public Result<Timestamp> convertToModel(LocalDateTime value, ValueContext 
 context) {
    Result<Timestamp> rs =  (Result<Timestamp>) Timestamp.valueOf(value);
    return rs;
}

@Override
public LocalDateTime convertToPresentation(Timestamp value, ValueContext 
 context) {
    // TODO Auto-generated method stub
    return null;
    }
}

This is giving error that Timestamp cannot be casted into Result

deep Kumar
  • 45
  • 7

2 Answers2

3

You have two possibilities:

  1. Evaluate to change the type of the date in your entity
  2. Manage the conversion of the dates

If you choose the second path, you have to know that the type managed by the Vaadin 8 DateTimeField is the Java 8 LocalDateTime. You can manage the conversion easily doing something like this:

LocalDateTime localDateTime = myField.getValue();
Timestamp timestamp = Timestamp.valueOf(localDateTime);

Then you will have the variable named timestamp as java.sql.Timestamp ready to be used in your entity.

UPDATE:

Since you are using a custom converter, you need to wrap the value into a Vaadin Converter Result. Casting your value into a Result is not a solution because they are objects of totally different types. You need instead to do something like this:

public Result<Timestamp> convertToModel(LocalDateTime value, ValueContext 
    context) {
    return Result.ok(Timestamp.valueOf(value));
}
  • Please look again. I am using customer convertor which is `consumerBinder.forField(upd_tmstmp) .withConverter(new StringTimestampConvertor()) .bind(Consumer::getUpd_tmstmp, Consumer::setUpd_tmstmp);` – deep Kumar Jan 03 '20 at 11:23
  • 2
    I've just added the response. Please be sure you have explained all about your problem since it can be confusing changing the question and the answer many times. – Christian Cavuti Jan 03 '20 at 13:46
2

This is giving error that Timestamp cannot be casted into Result

This is not a correct way of casting (You cannot cast a Timestamp class to a Result)

Result<Timestamp> rs = (Result<Timestamp>) Timestamp.valueOf(value);

You should do instead Result.ok(Timestamp.valueOf(value)); ( Result interface)

anasmi
  • 2,562
  • 1
  • 13
  • 25
  • i tried the same as mentioned above but now it is showing error that the argument 'value' in convertor class is null while entering the datetimeconsumer. This is how i am calling that function ` Binder.forField(upd_tmstmp) .withConverter(new StringTimestampConvertor()) .bind(Consumer::getUpd_tmstmp, Consumer::setUpd_tmstmp);` – deep Kumar Jan 06 '20 at 05:10
  • If your `value` is null, it's expected. Look at JavaDocs here :[`valueOf`](https://docs.oracle.com/javase/8/docs/api/java/sql/Timestamp.html#valueOf-java.time.LocalDateTime-). You should implement a proper null handling. – anasmi Jan 06 '20 at 09:13