如何获得从现在到未来时间的月,周,日和小时数? [英] How to get number of months, weeks, days and hours from now to a future time?

查看:157
本文介绍了如何获得从现在到未来时间的月,周,日和小时数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找出使用Java从现在到未来时间的剩余月数,周数,天数和小时数。我不能使用像Joda这样的任何第三方库。我怎么能只使用JDK类呢?

I need to find out the number of months, weeks, days and hours left from now to a future time using Java. I can not use any third party library like Joda. How can I do that using just JDK classes?

到目前为止,这是我想出的。除了某些情况外,它有用:

So far, this is what I have come up with. It sort of works, except for some situations:

public class DateUtil {

public static Integer[] components(Date from, Date to) {
    Integer[] result = new Integer[4];
    //SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    //df.setTimeZone(TimeZone.getTimeZone("EST"));

    Calendar fromCal = new GregorianCalendar();
    Calendar toCal = new GregorianCalendar();

    fromCal.setTime(from);
    toCal.setTime(to);

    int months = 0;
    do {
        fromCal.add(Calendar.MONTH, 1);
        ++months;
        //System.out.println(df.format(fromCal.getTime()));
    } while (fromCal.before(toCal));

    fromCal.add(Calendar.MONTH, -1);
    --months;

    int days = 0;
    do {
        fromCal.add(Calendar.DAY_OF_YEAR, 1);
        ++days;
    } while (fromCal.before(toCal));

    fromCal.add(Calendar.DAY_OF_YEAR, -1);
    --days;

    int hours = 0;
    do {
        fromCal.add(Calendar.HOUR_OF_DAY, 1);
        ++hours;
    } while (fromCal.before(toCal));

    fromCal.add(Calendar.HOUR_OF_DAY, -1);
    --hours;

    int minutes = 0;
    do {
        fromCal.add(Calendar.MINUTE, 1);
        ++minutes;
    } while (fromCal.before(toCal));

    result[0] = months;
    result[1] = days;
    result[2] = hours;
    result[3] = minutes;

    return result;
}

public static void main(String[] args) {
    try {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        df.setTimeZone(TimeZone.getTimeZone("EST"));

        Date from = df.parse("2014-03-29 00:00");
        Date to = df.parse("2014-05-29 00:00");
        Integer result[] = components(from, to);

        System.out.printf("Months:%02d Days:%02d Hrs:%02d Mins:%02d\n", 
                result[0], result[1], result[2], result[3]);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

当你出现不可接受的结果时2月在中间,开始日期是月末。例如:

It produces unacceptable results when you have February in the middle and start date is end of the month. For example:

来自:2013年12月31日
到:2014年12月31日

From: Dec 31, 2013 To: Dec 31, 2014

差异将产生:12个月,3天。

The difference will produce: 12 months, 3 days.

推荐答案

为什么不使用Java类日期日历

Why not use the Java classes Date and Calendar

这些类已经内置了功能来帮助你计算两个日期之间的差异。大多数Date方法似乎都被弃用了,所以我建议使用Calendar。祝你好运!

These classes already have built in functionality to help you calculate difference between two Dates. Most of the Date methods seem to be deprecated so instead I would recommend Calendar. Good luck!

这篇关于如何获得从现在到未来时间的月,周,日和小时数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆