Duration using Java.Time in Java8--itemPropertyChanged(Unknown Source) -

I am trying to use the Java 8 time functionalities and have a problem.

First needed the Util date converted into Instance followed by using the Duration API.

Here is my code in controller:


   if("startDate".equals(e.getProperty()) || "endDate".equals(e.getProperty())){
                LocalDate fDate = productionPlanResourceDs.getItem().getStartDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                LocalDate tDate = productionPlanResourceDs.getItem().getEndDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                long days = Duration.between(fDate, tDate).toDays();
                productionPlanResourceDs.getItem().setDuration(days);
                productionPlanResourceDs.refresh();
            }

I am getting the following error in the line …“LocalDate fDate = …”


java.lang.UnsupportedOperationException
	at java.sql.Date.toInstant(Date.java:304)
	at com.company.gms.gui.productionplan.ProductionPlanEdit.lambda$init$1(ProductionPlanEdit.java:91)
	at com.company.gms.gui.productionplan.ProductionPlanEdit$$Lambda$103/110614814.itemPropertyChanged(Unknown Source)
	at com.haulmont.cuba.gui.data.impl.AbstractDatasource$ItemListener.propertyChanged(AbstractDatasource.java:333)
	at com.haulmont.chile.core.model.impl.AbstractInstance.propertyChanged(AbstractInstance.java:46)
	at com.company.gms.entity.ProductionPlanResource.setStartDate(ProductionPlanResource.java:99)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at com.haulmont.chile.core.model.utils.MethodsCache.invokeSetter(MethodsCache.java:58)
	at com.haulmont.chile.core.model.impl.AbstractInstance.setValue(AbstractInstance.java:126)
	at com.haulmont.cuba.core.entity.BaseGenericIdEntity.setValue(BaseGenericIdEntity.java:228)
	at com.haulmont.chile.core.model.impl.AbstractInstance.setValue(AbstractInstance.java:112)
	at com.haulmont.chile.core.model.utils.InstanceUtils.setValueEx(InstanceUtils.java:178)
	at com.haulmont.cuba.web.gui.components.WebDateField.updateInstance(WebDateField.java:284)
	at com.haulmont.cuba.web.gui.components.WebDateField.lambda$createDateValueChangeListener$50693cdd$1(WebDateField.java:101)
	at com.haulmont.cuba.web.gui.components.WebDateField$$Lambda$89/836028012.valueChange(Unknown Source)
	at sun.reflect.GeneratedMethodAccessor101.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
	at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:198)
	at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161)
	at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:1008)
	at com.vaadin.ui.AbstractField.fireValueChange(AbstractField.java:1169)
	at com.vaadin.ui.DateField.fireValueChange(DateField.java:643)
	at com.vaadin.ui.AbstractField.setValue(AbstractField.java:577)
	at com.vaadin.ui.AbstractField.setValue(AbstractField.java:473)
	at com.vaadin.ui.DateField.setValue(DateField.java:719)
	at com.haulmont.cuba.web.toolkit.ui.CubaDateField.setValue(CubaDateField.java:66)
	at com.vaadin.ui.DateField.changeVariables(DateField.java:618)
	at com.haulmont.cuba.web.toolkit.ui.CubaDateField.changeVariables(CubaDateField.java:74)
	at com.vaadin.server.communication.ServerRpcHandler.changeVariables(ServerRpcHandler.java:609)
	at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:428)
	at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:274)
	at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:79)
	at com.haulmont.cuba.web.sys.CubaVaadinServletService$CubaUidlRequestHandler.lambda$synchronizedHandleRequest$82(CubaVaadinServletService.java:306)
	at com.haulmont.cuba.web.sys.CubaVaadinServletService$CubaUidlRequestHandler$$Lambda$27/391421285.call(Unknown Source)
	at com.haulmont.cuba.web.sys.CubaVaadinServletService.withUserSession(CubaVaadinServletService.java:187)
	at com.haulmont.cuba.web.sys.CubaVaadinServletService$CubaUidlRequestHandler.synchronizedHandleRequest(CubaVaadinServletService.java:306)
	at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
	at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1409)
	at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:369)
	at com.haulmont.cuba.web.sys.CubaApplicationServlet.serviceAppRequest(CubaApplicationServlet.java:242)
	at com.haulmont.cuba.web.sys.CubaApplicationServlet.service(CubaApplicationServlet.java:165)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Hi,
The described exception always occurs when the java.sql.Date.toInstant() method is called.
Use java.util.Date for the ‘StartDate’ - attribute of your entity and the code should work.

Hi
I didn’t use java.sql.Date.

Used java.util.Date and java.time


  java.util.Date d1=productionPlanResourceDs.getItem().getStartDate();
                java.util.Date d2=productionPlanResourceDs.getItem().getEndDate();
                long days = Duration.between(d1, d2).toDays();

The last line above throughs exception – Temporal in Duration can’t be applied to java.util.Date

Do you have any code that worked to get the duration?

Hi
I checked it again with documentations and found the codes that I have used is correct but it’s throughing unsupportedOpetaionException


java.lang.UnsupportedOperationException
	at java.sql.Date.toInstant(Date.java:304)
	at com.company.gms.gui.productionplan.ProductionPlanEdit.lambda$init$61(ProductionPlanEdit.java:99)
	at com.company.gms.gui.productionplan.ProductionPlanEdit$$Lambda$87/1215564899.itemPropertyChanged(Unknown Source)
	at com.haulmont.cuba.gui.data.impl.AbstractDatasource$ItemListener.propertyChanged(AbstractDatasource.java:333)
	at com.haulmont.chile.core.model.impl.AbstractInstance.propertyChanged(AbstractInstance.java:46)
	at com.company.gms.entity.ProductionPlanResource.setStartDate(ProductionPlanResource.java:114)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

When I compile, no error is found though.

Is this something not supported in CUBA or there is an alternative way of coding?

Hi
Can anyone tell me what’s not right in my code? Thanks in advance.

Looks like the new version of the Studio is now updated to work with java.sql.time and Date! Will have to give a fresh try…


Version 2.2.2

Improvements, including:

Support for java.sql.Time and java.sql.Date entity attribute types.

Hi Mortoza,
These are not new Java 8 types, but old JDBC ones. The platform supports them to specify date-only and time-only values instead of using @Temporal annotation. And now Studio understands them too, but this is just for compatibility with manually created entities.