【问题标题】:How to get number of months, weeks, days and hours from now to a future time?如何获取从现在到未来时间的月数、周数、天数和小时数?
【发布时间】:2013-11-13 19:08:45
【问题描述】:

我需要使用 Java 找出从现在到未来的月数、周数、天数和小时数。我不能使用像 Joda 这样的第三方库。如何仅使用 JDK 类来做到这一点?

到目前为止,这就是我想出的。它有点工作,除了某些情况:

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();
    }
}
}

当二月在中间并且开始日期是月底时,它会产生不可接受的结果。例如:

自:2013 年 12 月 31 日 至:2014 年 12 月 31 日

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

【问题讨论】:

  • 你为什么不能使用 Joda?
  • 您可以尝试查看 this examplethis examplethis example 以获得一些想法
  • 我认为对于那些不了解本网站规范的新手来说,不需要投票。预计会在此处发布带有一些演示的帖子,新来者在被否决之前永远不会知道这一点。
  • 请参考并进一步增强您的逻辑。 mkyong.com/java/how-to-calculate-date-time-difference-in-java
  • RajV,你错了。 Mike W 指出您没有在问题中提供足够的信息是绝对正确的,他并没有发表刻薄的评论。他不是读心术,如果你不告诉他,他不知道你在使用日期和日历方面有多棒。可以对 Polywhirl 先生提供的解决方案进行简单修改以解决您的用例。如果您需要这方面的帮助,请寻求帮助。不要说他的贡献是无用的,它只会让你看起来像那种没人愿意帮助的人。

标签: java


【解决方案1】:

为什么不使用 Java 类 DateCalendar

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

【讨论】:

  • +1 提倡使用 Calendar 而不是已弃用的 Date 类。
  • Date 什么时候被弃用了?它的许多方法都有,但类本身没有。
【解决方案2】:

我的一位同事最终想出了正确的答案。将月份增量添加到日历的问题在于,您从 1 月 31 日到 2 月 28 日再到 3 月 28 日,而不是 3 月 31 日。这增加了不准确性。解决方法是在原开始日期的基础上增加1个月、2个月、3个月等。然后你从 1 月 31 日到 2 月 28 日到 3 月 31 日。无论如何,这是他的解决方案,我稍作改动。

public class TimeSpan {
    public int months;
    public int days;
    public int hours;
    public int minutes;
}

class DateCalculator {
    public static TimeSpan difference(Date later, Date earlier) {
        TimeSpan v = new TimeSpan();

        /* Add months until we go past the target, then go back one. */
        while (calculateOffset(earlier, v).compareTo(later) <= 0) {
            v.months++;
        }
        v.months--;

        /* Add days until we go past the target, then go back one. */
        while (calculateOffset(earlier, v).compareTo(later) <= 0) {
            v.days++;
        }
        v.days--;

        /* Add hours until we go past the target, then go back one. */
        while (calculateOffset(earlier, v).compareTo(later) <= 0) {
            v.hours++;
        }
        v.hours--;
        while (calculateOffset(earlier, v).compareTo(later) <= 0) {
            v.minutes++;
        }
        v.minutes--;

        return v;
    }

    private static Date calculateOffset(Date start, TimeSpan offset) {
        Calendar c = new GregorianCalendar();

        c.setTime(start);

        c.add(Calendar.MONTH, offset.months);
        c.add(Calendar.DAY_OF_YEAR, offset.days);
        c.add(Calendar.HOUR, offset.hours);
        c.add(Calendar.MINUTE, offset.minutes);

        return c.getTime();
    }
     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("2013-01-31 00:00");
                    Date to = df.parse("2014-01-31 10:20");
                    TimeSpan ts = difference(to, from);

                    System.out.printf("Months:%02d Days:%02d Hrs:%02d Mins:%02d\n",
                                    ts.months, ts.days, ts.hours, ts.minutes);
            } catch (Exception e) {
                    e.printStackTrace();
            }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多