java如何计算时间_如何在Java中计算“时间前”?

public class TimeUtils {

public final static long ONE_SECOND = 1000;

public final static long SECONDS = 60;

public final static long ONE_MINUTE = ONE_SECOND * 60;

public final static long MINUTES = 60;

public final static long ONE_HOUR = ONE_MINUTE * 60;

public final static long HOURS = 24;

public final static long ONE_DAY = ONE_HOUR * 24;

private TimeUtils() {

}

/**

* converts time (in milliseconds) to human-readable format

*  " days,  hours,  minutes and (z) seconds"

*/

public static String millisToLongDHMS(long duration) {

StringBuffer res = new StringBuffer();

long temp = 0;

if (duration >= ONE_SECOND) {

temp = duration / ONE_DAY;

if (temp > 0) {

duration -= temp * ONE_DAY;

res.append(temp).append(" day").append(temp > 1 ? "s" : "")

.append(duration >= ONE_MINUTE ? ", " : "");

}

temp = duration / ONE_HOUR;

if (temp > 0) {

duration -= temp * ONE_HOUR;

res.append(temp).append(" hour").append(temp > 1 ? "s" : "")

.append(duration >= ONE_MINUTE ? ", " : "");

}

temp = duration / ONE_MINUTE;

if (temp > 0) {

duration -= temp * ONE_MINUTE;

res.append(temp).append(" minute").append(temp > 1 ? "s" : "");

}

if (!res.toString().equals("") && duration >= ONE_SECOND) {

res.append(" and ");

}

temp = duration / ONE_SECOND;

if (temp > 0) {

res.append(temp).append(" second").append(temp > 1 ? "s" : "");

}

return res.toString();

} else {

return "0 second";

}

}

public static void main(String args[]) {

System.out.println(millisToLongDHMS(123));

System.out.println(millisToLongDHMS((5 * ONE_SECOND) + 123));

System.out.println(millisToLongDHMS(ONE_DAY + ONE_HOUR));

System.out.println(millisToLongDHMS(ONE_DAY + 2 * ONE_SECOND));

System.out.println(millisToLongDHMS(ONE_DAY + ONE_HOUR + (2 * ONE_MINUTE)));

System.out.println(millisToLongDHMS((4 * ONE_DAY) + (3 * ONE_HOUR)

+ (2 * ONE_MINUTE) + ONE_SECOND));

System.out.println(millisToLongDHMS((5 * ONE_DAY) + (4 * ONE_HOUR)

+ ONE_MINUTE + (23 * ONE_SECOND) + 123));

System.out.println(millisToLongDHMS(42 * ONE_DAY));

/*

output :

0 second

5 seconds

1 day, 1 hour

1 day and 2 seconds

1 day, 1 hour, 2 minutes

4 days, 3 hours, 2 minutes and 1 second

5 days, 4 hours, 1 minute and 23 seconds

42 days

*/

}}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值