1 2 3 4 5 6 7 8 9 10 11 12 13 14 | DateFormat df = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); try { Date d1 = df.parse( "2004-03-26 13:31:40" ); Date d2 = df.parse( "2004-01-02 11:30:24" ); long diff = d1.getTime() - d2.getTime(); //这样得到的差值是微秒级别 long days = diff / ( 1000 * 60 * 60 * 24 ); long hours = (diff-days*( 1000 * 60 * 60 * 24 ))/( 1000 * 60 * 60 ); long minutes = (diff-days*( 1000 * 60 * 60 * 24 )-hours*( 1000 * 60 * 60 ))/( 1000 * 60 ); System.out.println( "" +days+ "天" +hours+ "小时" +minutes+ "分" ); } catch (Exception e) { } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步