Java8 还使用joda计算相差多少天、多少小时、多少分钟?

Java8 还使用joda计算相差多少天、多少小时?

  • joda应用背景
    • joda Interval代码
    • 测试环境出现的异常
    • 其他问题
  • 使用java8 ZonedDateTime替换joda Interval
    • java8 解决方法代码

joda应用背景

最近兼职开发某一个网站中,需要计算出数据库创建时间跟当前系统时间相差多少天、多少小时的业务场景,数据库为mysql,字段类型为timestamp ,ORM使用mybatis,java对应类型为date。网站运行一段时间,开发、测试、生产都没有一点问题,前几天在测试环境报错了。被甲方鄙视一番,都java8了,还用第三方日期jar了。。。心里翻滚了N个白眼,废话不多说了,直接上代码。
版本:joda-time2.9.9

joda Interval代码

 //发布时间
   Interval interval = new Interval(recruitInfo.getCreatedAt().getTime(), (new Date()).getTime());
   org.joda.time.Period p = interval.toPeriod();
   //""+p.getDays()+" 天 " + p.getHours()+ " 小时 "+p.getMinutes()+" 分钟"+p.getSeconds()+" 秒"
   if (Math.abs(p.getDays()) > 0) {
       result.setCreateTime(Math.abs(p.getDays()) + "天前");
   } else if (Math.abs(p.getHours()) > 0) {
       result.setCreateTime(Math.abs(p.getHours()) + "小时前");
   } else {
       result.setCreateTime("刚刚");
   }

测试环境出现的异常

java.lang.IllegalArgumentException: The end instant must be greater than the start instant
at org.joda.time.base.AbstractInterval.checkInterval(AbstractInterval.java:63) ~[joda-time-2.9.9.jar:2.9.9]
at org.joda.time.base.BaseInterval.(BaseInterval.java:73) ~[joda-time-2.9.9.jar:2.9.9]
at org.joda.time.Interval.(Interval.java:159) ~[joda-time-2.9.9.jar:2.9.9]
at com.hupo.fangzhuo.service.RecruitService.queryRecruitById(RecruitService.java:312) ~[service-0.1.0-SNAPSHOT.jar:na]
at com.hupo.fangzhuo.service.RecruitService F a s t C l a s s B y S p r i n g C G L I B FastClassBySpringCGLIB FastClassBySpringCGLIB8b52da6d.invoke() ~[service-0.1.0-SNAPSHOT.jar:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.aop.framework.CglibAopProxy D y n a m i c A d v i s e d I n t e r c e p t o r . i n t e r c e p t ( C g l i b A o p P r o x y . j a v a : 685 )   [ s p r i n g − a o p − 5.0.5. R E L E A S E . j a r : 5.0.5. R E L E A S E ] a t c o m . h u p o . f a n g z h u o . s e r v i c e . R e c r u i t S e r v i c e DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:685) ~[spring-aop-5.0.5.RELEASE.jar:5.0.5.RELEASE] at com.hupo.fangzhuo.service.RecruitService DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:685) [springaop5.0.5.RELEASE.jar:5.0.5.RELEASE]atcom.hupo.fangzhuo.service.RecruitService E n h a n c e r B y S p r i n g C G L I B EnhancerBySpringCGLIB EnhancerBySpringCGLIB$11fa8626.queryRecruitById() ~[service-0.1.0-SNAPSHOT.jar:na]
at com.hupo.fangzhuo.controller.RecruitController.gain(RecruitController.java:139) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_111]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_111]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_111]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877) ~[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783) ~[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) ~[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) ~[spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974) [spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866) [spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) [servlet-api.jar:na]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851) [spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) [servlet-api.jar:na]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [catalina.jar:9.0.10]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:9.0.10]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) [tomcat-websocket.jar:9.0.10]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:9.0.10]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:9.0.10]
at com.hupo.ft.onepunch.core.security.AppTraceFilter.doFilter(AppTraceFilter.java:134) [core-java-0.2.60-SNAPSHOT.jar:na]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:9.0.10]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:9.0.10]
at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123) [druid-1.1.10.jar:1.1.10]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:9.0.10]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:9.0.10]
at com.hupo.service.spring.common.access.AccessFilter.doFilter(AccessFilter.java:84) [access-log-0.3.45-SNAPSHOT.jar:na]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:9.0.10]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:9.0.10]
at com.hupo.service.spring.common.access.HttpBodyTraceFilter.doFilter(HttpBodyTraceFilter.java:133) [access-log-0.3.45-SNAPSHOT.jar:na]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:9.0.10]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:9.0.10]
at com.hupo.service.spring.common.filter.CorsFilter.doFilter(CorsFilter.java:34) [common-spring-0.3.45-SNAPSHOT.jar:na]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:9.0.10]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:9.0.10]
at com.hupo.fangzhuo.filter.GlobalExceptionHandlerFilter.doFilter(GlobalExceptionHandlerFilter.java:35) [classes/:na]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:9.0.10]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:9.0.10]
at org.springframework.boot.web.servlet.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:117) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.web.servlet.support.ErrorPageFilter.access$000(ErrorPageFilter.java:61) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.web.servlet.support.ErrorPageFilter 1. d o F i l t e r I n t e r n a l ( E r r o r P a g e F i l t e r . j a v a : 92 ) [ s p r i n g − b o o t − 2.0.1. R E L E A S E . j a r : 2.0.1. R E L E A S E ] a t o r g . s p r i n g f r a m e w o r k . w e b . f i l t e r . O n c e P e r R e q u e s t F i l t e r . d o F i l t e r ( O n c e P e r R e q u e s t F i l t e r . j a v a : 107 ) [ s p r i n g − w e b − 5.0.5. R E L E A S E . j a r : 5.0.5. R E L E A S E ] a t o r g . s p r i n g f r a m e w o r k . b o o t . w e b . s e r v l e t . s u p p o r t . E r r o r P a g e F i l t e r . d o F i l t e r ( E r r o r P a g e F i l t e r . j a v a : 110 ) [ s p r i n g − b o o t − 2.0.1. R E L E A S E . j a r : 2.0.1. R E L E A S E ] a t o r g . a p a c h e . c a t a l i n a . c o r e . A p p l i c a t i o n F i l t e r C h a i n . i n t e r n a l D o F i l t e r ( A p p l i c a t i o n F i l t e r C h a i n . j a v a : 193 ) [ c a t a l i n a . j a r : 9.0.10 ] a t o r g . a p a c h e . c a t a l i n a . c o r e . A p p l i c a t i o n F i l t e r C h a i n . d o F i l t e r ( A p p l i c a t i o n F i l t e r C h a i n . j a v a : 166 ) [ c a t a l i n a . j a r : 9.0.10 ] a t o r g . s p r i n g f r a m e w o r k . w e b . f i l t e r . C h a r a c t e r E n c o d i n g F i l t e r . d o F i l t e r I n t e r n a l ( C h a r a c t e r E n c o d i n g F i l t e r . j a v a : 200 ) [ s p r i n g − w e b − 5.0.5. R E L E A S E . j a r : 5.0.5. R E L E A S E ] a t o r g . s p r i n g f r a m e w o r k . w e b . f i l t e r . O n c e P e r R e q u e s t F i l t e r . d o F i l t e r ( O n c e P e r R e q u e s t F i l t e r . j a v a : 107 ) [ s p r i n g − w e b − 5.0.5. R E L E A S E . j a r : 5.0.5. R E L E A S E ] a t o r g . a p a c h e . c a t a l i n a . c o r e . A p p l i c a t i o n F i l t e r C h a i n . i n t e r n a l D o F i l t e r ( A p p l i c a t i o n F i l t e r C h a i n . j a v a : 193 ) [ c a t a l i n a . j a r : 9.0.10 ] a t o r g . a p a c h e . c a t a l i n a . c o r e . A p p l i c a t i o n F i l t e r C h a i n . d o F i l t e r ( A p p l i c a t i o n F i l t e r C h a i n . j a v a : 166 ) [ c a t a l i n a . j a r : 9.0.10 ] a t o r g . a p a c h e . c a t a l i n a . c o r e . S t a n d a r d W r a p p e r V a l v e . i n v o k e ( S t a n d a r d W r a p p e r V a l v e . j a v a : 199 ) [ c a t a l i n a . j a r : 9.0.10 ] a t o r g . a p a c h e . c a t a l i n a . c o r e . S t a n d a r d C o n t e x t V a l v e . i n v o k e ( S t a n d a r d C o n t e x t V a l v e . j a v a : 96 ) [ c a t a l i n a . j a r : 9.0.10 ] a t o r g . a p a c h e . c a t a l i n a . a u t h e n t i c a t o r . A u t h e n t i c a t o r B a s e . i n v o k e ( A u t h e n t i c a t o r B a s e . j a v a : 491 ) [ c a t a l i n a . j a r : 9.0.10 ] a t o r g . a p a c h e . c a t a l i n a . c o r e . S t a n d a r d H o s t V a l v e . i n v o k e ( S t a n d a r d H o s t V a l v e . j a v a : 139 ) [ c a t a l i n a . j a r : 9.0.10 ] a t o r g . a p a c h e . c a t a l i n a . v a l v e s . E r r o r R e p o r t V a l v e . i n v o k e ( E r r o r R e p o r t V a l v e . j a v a : 92 ) [ c a t a l i n a . j a r : 9.0.10 ] a t o r g . a p a c h e . c a t a l i n a . v a l v e s . A b s t r a c t A c c e s s L o g V a l v e . i n v o k e ( A b s t r a c t A c c e s s L o g V a l v e . j a v a : 668 ) [ c a t a l i n a . j a r : 9.0.10 ] a t o r g . a p a c h e . c a t a l i n a . c o r e . S t a n d a r d E n g i n e V a l v e . i n v o k e ( S t a n d a r d E n g i n e V a l v e . j a v a : 87 ) [ c a t a l i n a . j a r : 9.0.10 ] a t o r g . a p a c h e . c a t a l i n a . c o n n e c t o r . C o y o t e A d a p t e r . s e r v i c e ( C o y o t e A d a p t e r . j a v a : 343 ) [ c a t a l i n a . j a r : 9.0.10 ] a t o r g . a p a c h e . c o y o t e . h t t p 11. H t t p 11 P r o c e s s o r . s e r v i c e ( H t t p 11 P r o c e s s o r . j a v a : 408 ) [ t o m c a t − c o y o t e . j a r : 9.0.10 ] a t o r g . a p a c h e . c o y o t e . A b s t r a c t P r o c e s s o r L i g h t . p r o c e s s ( A b s t r a c t P r o c e s s o r L i g h t . j a v a : 66 ) [ t o m c a t − c o y o t e . j a r : 9.0.10 ] a t o r g . a p a c h e . c o y o t e . A b s t r a c t P r o t o c o l 1.doFilterInternal(ErrorPageFilter.java:92) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.springframework.boot.web.servlet.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:110) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:9.0.10] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:9.0.10] at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) [spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:9.0.10] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:9.0.10] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) [catalina.jar:9.0.10] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [catalina.jar:9.0.10] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:491) [catalina.jar:9.0.10] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [catalina.jar:9.0.10] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [catalina.jar:9.0.10] at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668) [catalina.jar:9.0.10] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [catalina.jar:9.0.10] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [catalina.jar:9.0.10] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) [tomcat-coyote.jar:9.0.10] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-coyote.jar:9.0.10] at org.apache.coyote.AbstractProtocol 1.doFilterInternal(ErrorPageFilter.java:92)[springboot2.0.1.RELEASE.jar:2.0.1.RELEASE]atorg.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)[springweb5.0.5.RELEASE.jar:5.0.5.RELEASE]atorg.springframework.boot.web.servlet.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:110)[springboot2.0.1.RELEASE.jar:2.0.1.RELEASE]atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)[catalina.jar:9.0.10]atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)[catalina.jar:9.0.10]atorg.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)[springweb5.0.5.RELEASE.jar:5.0.5.RELEASE]atorg.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)[springweb5.0.5.RELEASE.jar:5.0.5.RELEASE]atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)[catalina.jar:9.0.10]atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)[catalina.jar:9.0.10]atorg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)[catalina.jar:9.0.10]atorg.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)[catalina.jar:9.0.10]atorg.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:491)[catalina.jar:9.0.10]atorg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)[catalina.jar:9.0.10]atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)[catalina.jar:9.0.10]atorg.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)[catalina.jar:9.0.10]atorg.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)[catalina.jar:9.0.10]atorg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)[catalina.jar:9.0.10]atorg.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)[tomcatcoyote.jar:9.0.10]atorg.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)[tomcatcoyote.jar:9.0.10]atorg.apache.coyote.AbstractProtocolConnectionHandler.process(AbstractProtocol.java:764) [tomcat-coyote.jar:9.0.10]
at org.apache.tomcat.util.net.NioEndpoint S o c k e t P r o c e s s o r . d o R u n ( N i o E n d p o i n t . j a v a : 1388 ) [ t o m c a t − c o y o t e . j a r : 9.0.10 ] a t o r g . a p a c h e . t o m c a t . u t i l . n e t . S o c k e t P r o c e s s o r B a s e . r u n ( S o c k e t P r o c e s s o r B a s e . j a v a : 49 ) [ t o m c a t − c o y o t e . j a r : 9.0.10 ] a t j a v a . u t i l . c o n c u r r e n t . T h r e a d P o o l E x e c u t o r . r u n W o r k e r ( T h r e a d P o o l E x e c u t o r . j a v a : 1142 ) [ n a : 1.8. 0 1 11 ] a t j a v a . u t i l . c o n c u r r e n t . T h r e a d P o o l E x e c u t o r SocketProcessor.doRun(NioEndpoint.java:1388) [tomcat-coyote.jar:9.0.10] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-coyote.jar:9.0.10] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_111] at java.util.concurrent.ThreadPoolExecutor SocketProcessor.doRun(NioEndpoint.java:1388)[tomcatcoyote.jar:9.0.10]atorg.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)[tomcatcoyote.jar:9.0.10]atjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)[na:1.8.0111]atjava.util.concurrent.ThreadPoolExecutorWorker.run(ThreadPoolExecutor.java:617) [na:1.8.0_111]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-util.jar:9.0.10]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111]

通过异常,查找源码知道是joda构造函数报的。具体报错是在:
Java8 还使用joda计算相差多少天、多少小时、多少分钟?_第1张图片
原因:第二个参数值比第一个参数值小,知道问题就好办了。出现这个情况明显的原因就是应用服务器时间和数据库服务器的时间,不一致导致。最快的解决方法,就是叫甲方运营更改下服务器时间即可,但是我不是甲方人员,甲方运维人员才不会听你一个兼职人员的话了。此条路行不通,就只能使用其他解决手段了。我在想那好办啊,在构造方法之前,添加了一个if,判断下两个值谁大谁小,最后交换下前后位置,再传输至构造函数中,然后提交git merge,最后管理git的人,一看怎么还是用joda,马上就怼我了。。。于是就出现了后续的解决方案。

其他问题

1、我们知道,对于mysql timestamp,他从客户端入库的时间会转化为UTC(世界标准时间)进行存储,时间存储范围是1970-01-01至2038-01-19,如果业务方需求需要显示小时、分钟、秒的话,因为没有时区的概念,则会少8个小时的情况出现。

使用java8 ZonedDateTime替换joda Interval

通过此业务场景,我们知道joda已经存在时区和服务器时间不一致问题。知道问题,对症下药,后面就是依葫芦画瓢的事,问题就迎刃而解了。代码如下:

java8 解决方法代码

  ZonedDateTime nowTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis()), DateTimeUtils.cnZoneOffset());
        ZonedDateTime beginTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(recruitInfo.getCreatedAt().toInstant().toEpochMilli()), DateTimeUtils.cnZoneOffset());
//        long days = nowTime.toLocalDate().toEpochDay() - beginTime.toLocalDate().toEpochDay();
//        long days = ChronoUnit.DAYS.between(beginTime.toLocalDate(), nowTime.toLocalDate());
        Duration duration = Duration.between(nowTime, beginTime).abs();
        long days = duration.toDays();
        long hours = duration.toHours();
        if (days > 0) {
            result.setCreateTime(days + "天前");
        } else if (hours > 0) {
            result.setCreateTime(hours + "小时前");
        } else {
            result.setCreateTime("刚刚");
        }

至此问题解决。

你可能感兴趣的:(经验分享,后端)