差值是12的倍数java中怎么写_如何在Java中找到两个日期之间的差值的持续时间?...

publicstaticStringgetDurationTimeStamp(Stringdate){StringtimeDifference="";//date formatter as per the coder needSimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");//parse the string date-ti// me to Date objectDatestartDate=null;try{startDate=sdf.parse(date);}catch(ParseExceptione){e.printStackTrace();}//end date will be the current system time to calculate the lapse time difference//if needed, coder can add end date to whatever dateDateendDate=newDate();System.out.println(startDate);System.out.println(endDate);//get the time difference in millisecondslongduration=endDate.getTime()-startDate.getTime();//now we calculate the differences in different time units//this long value will be the total time difference in each unit//i.e; total difference in seconds, total difference in minutes etc...longdiffInSeconds=TimeUnit.MILLISECONDS.toSeconds(duration);longdiffInMinutes=TimeUnit.MILLISECONDS.toMinutes(duration);longdiffInHours=TimeUnit.MILLISECONDS.toHours(duration);longdiffInDays=TimeUnit.MILLISECONDS.toDays(duration);//now we create the time stamps depending on the value of each unit that we get//as we do not have the unit in years,//we will see if the days difference is more that 365 days, as 365 days = 1 yearif(diffInDays>365){//we get the year in integer not in float//ex- 791/365 = 2.167 in float but it will be 2 years in intintyear=(int)(diffInDays/365);timeDifference=year+" years ago";System.out.println(year+" years ago");}//if days are not enough to create year then get the dayselseif(diffInDays>1){timeDifference=diffInDays+" days ago";System.out.println(diffInDays+" days ago");}//if days value<1 then get the hourselseif(diffInHours>1){timeDifference=diffInHours+" hours ago";System.out.println(diffInHours+" hours ago");}//if hours value<1 then get the minuteselseif(diffInMinutes>1){timeDifference=diffInMinutes+" minutes ago";System.out.println(diffInMinutes+" minutes ago");}//if minutes value<1 then get the secondselseif(diffInSeconds>1){timeDifference=diffInSeconds+" seconds ago";System.out.println(diffInSeconds+" seconds ago");}returntimeDifference;// that's all. Happy Coding :)}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值