简体   繁体   English

Java + Joda Time两个日期之间的一周

[英]Week between two dates Java + Joda Time

I wanted to get the number of weeks and months between two date range in Java. 我希望在Java中获得两个日期范围之间的周数和月数。 For ex., Start Date: 03/01/2012 End Date: 03/05/2012 例如,开始日期:03/01/2012结束日期:03/05/2012

Since the two dates fall in two different weeks I want the result to be 2 instead of 0. 由于这两个日期分为两个不同的星期,我希望结果为2而不是0。

Second part of the problem is: Start Date: 02/29/2012 End Date: 03/01/2012 问题的第二部分是:开始日期:02/29/2012结束日期:03/01/2012

Number of months in between should be 2. 介于两者之间的月数应为2。

I have been searching online regarding this and lot of people have recommended using Joda Date time in Java. 我一直在网上搜索这个,很多人都推荐在Java中使用Joda Date时间。 So I gave it a shot. 所以我试了一下。 I was able to get the weeks working but I am not sure if this is the right way. 我能够让这几周工作,但我不确定这是否正确。 Here is what I am doing to get the week duration: 这是我为了获得一周的持续时间而做的事情:

    DateTime s = new DateTime(Long.parseLong("1330573027000")); // 2012-02-29
    DateTime e = new DateTime(Long.parseLong("1331005027000")); // 2012-03-05   

    Weeks weeks = Weeks.weeksBetween(s, e).plus(1);

This returns 1, when I am expecting 2 since two dates are in different weeks. 当我期待2时,这会返回1,因为两个日期在不同的星期。

For months duration I tried to follow the same but it returns 0 but I want it to return 2 since the two dates are in two different months. 几个月的持续时间我尝试遵循相同但它返回0但我希望它返回2,因为这两个日期是在两个不同的月份。

Could someone please point me in right direction? 有人可以指点我正确的方向吗?

Thanks! 谢谢!

Edit: I think I got one way of doing it, please let me know if it looks right: 编辑:我想我有一种做法,请告诉我它是否正确:

    DateTime start = new DateTime(Long.parseLong("1330659427000"));
    DateTime start = new DateTime(Long.parseLong("1331005027000"));

    DateTime finalStart = start.dayOfWeek().withMinimumValue();
    DateTime finalEnd   = end.dayOfWeek().withMaximumValue();

And then get the difference between finalStart and finalEnd. 然后得到finalStart和finalEnd之间的区别。 Does this looks correct? 这看起来是否正确?

Edit2 Updated the end time Edit2更新了结束时间

JodaTime Weeks.weeksBetween(s, e) returns only whole week count. JodaTime Weeks.weeksBetween(s,e)仅返回整周计数。 Incomplete weeks are not counted. 不完整的周数不计算在内。 To resolve this, you must garantee that the days are at the start and at the end of the week. 要解决此问题,您必须保证日期在开始时和结束时。 Try this: 试试这个:

int weeks = Weeks.weeksBetween(s.dayOfWeek().withMinimumValue().minusDays(1), 
            e.dayOfWeek().withMaximumValue().plusDays(1)).getWeeks();

The minusDays/plusDays will garantee that the weeks i'm trying to count are full. minusDays / plusDays将保证我试图计算的周数已满。

Same logic apply for Months: 适用于月份的逻辑相同:

int months = Months.monthsBetween(s.dayOfMonth().withMinimumValue().minusDays(1),
             e.dayOfMonth().withMaximumValue().plusDays(1)).getMonths();
    int OFFSET_ONE = 1;

    DateTime t1 = new DateTime().withDate(2012, 02, 29).withDayOfWeek(1);
    DateTime t2 = new DateTime().withDate(2012, 03, 05).withDayOfWeek(7);

    int week1 = t2.weekOfWeekyear().get(); 
    int week2 = t1.weekOfWeekyear().get();

    System.out.println(week1-week2+OFFSET_ONE); // add OFFSET_ONE

does it give what you are after? 它会给你的东西吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM