Java:计算与当前时间相比的剩余时间

时间:2015-09-11 12:28:13

标签: java

我有一个格式的字符串:

2015-10-01 02:00

我想打印与Java中当前时间相比的剩余时间,它应该以格式打印:

It remains 1 day 4 hours 25 minutes 

我怎么能这样做?谢谢你的任何想法。

1 个答案:

答案 0 :(得分:4)

我搞定了!你能撤回所有未投票吗?

public static void calculateRemainTime(String scheduled_date){

    // date format
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    // two dates
    java.util.Date scheduledDate;
    Calendar current = Calendar.getInstance();
    java.util.Date currentDate;
    String current_date = format.format(current.getTime());
        try {
            scheduledDate = format.parse(scheduled_date);
            currentDate = format.parse(current_date);
            long diffInMillies = scheduledDate.getTime() - currentDate.getTime();
            long diffence_in_minute = TimeUnit.MINUTES.convert(diffInMillies,TimeUnit.MILLISECONDS);
            System.out.println(diffence_in_minute);
        } catch (ParseException e) {
            e.printStackTrace();
        }
}