如何计算两个LocalTime之间的差异并保存在要保存在银行的变量中?



我正在设置一个系统,用户将在其中插入一个事件(动作)的进入时间和离开时间,当他给出帖子时,我的系统将计算两个时间之间的差异并保存在可变持续时间内。我不知道怎么做,所以我试了一下

@PrePersistpublic Acao cullodifference () {

LocalDateTime inicio = LocalDateTime.of(entrada.getYear(), entrada.getMonth(), entrada.getDayOfMonth(),
entrada.getHour(), entrada.getMinute(), entrada.getSecond());
LocalDateTime fim = LocalDateTime.of(saida.getYear(), saida.getMonth(), saida.getDayOfMonth(), saida.getHour(),
saida.getMinute(), saida.getSecond());
Duration duration = Duration.between(inicio, fim);
duracao = duration;

System.out.println(duration);
System.out.println("Days between " + inicio + "e" + fim + ":" + duration.toHours());
return this;
}

那么如何计算它在可变期限内被保存,然后被保存在银行?

我认为这将是更好的LocalDateTime,因为我可能有一个问题,因为会有事件通过从一天到下一个?例如:start on 2021-02-02 23:55:32 end on 2021-02-03 00:02:21

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is java.lang.RuntimeException: Callback methods annotated on the bean class must return void and take no arguments: javax.persistence.PrePersist - public br.com.lucas.entity.Acao br.com.lucas.entity.Acao.calculoDiferenca()
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1788) ~[spring-beans-5.3.3.jar:5.3.3]

如果您需要查看更多部分,请遵循我的代码:https://github.com/Akssasori/ApiRestfull-Merchandising

您可以使用localDateOne.toEpochDay() - localDateTwo.toEpochDay();来获取日期之间的差异(以长为单位)。然后你可以从difference:

获取Locadate
LocalDate.ofEpochDay(localDateOne.toEpochDay() - localDateTwo.toEpochDay());

这种方法存在于Duration类本身:

Duration duration = Duration.between(inicio, fim);

最新更新