DateField<LocalDateTime> not working in p7

After migrating to v7.0 i wanted to migrate to java.time from java.util.Date, but ran into this:

@Inject
DateField<LocalDateTime> dateField;

throws ClassCastException: java.util.Date cannot be cast to java.time.LocalDateTime

it seems the datatype is not set properly during initialization of the component. Has someone had this issue?

Hi,

By default, DateField uses java.util.Date type. If you want to use LocalDateTime you have to bind field to LocalDateTime property or specify datatype explicitly:

@Inject
private DateField<LocalDateTime> demoDateField;

@Subscribe
protected void onInit(InitEvent event) {
    demoDateField.setDatatype(Datatypes.get(LocalDateTime.class));
}

It is required because @Inject field is not declaration of the component, it only wires already configured UI component to a screen controller.

1 Like

Thank you for the fast and clear answer Yuriy.

Is there a specific reason why java.util.Date is the default and not java.time? From what i see the below component WebDatefield work entirely based on java.time and then back converted to the older Date class for the default field setup.

We cannot change the default, it will completely break compatibility. It was java.util.Date and it stays the same in 7.0.0