简体   繁体   English

如何使用Joda-Time获取给定月份内的天数?

[英]How do I obtain the number of days within a given month using Joda-Time?

30 days hath September,
   April, June and November,
 All the rest have 31,
   Excepting February alone
(And that has 28 days clear,
   With 29 in each leap year).

Can I obtain this info anagrammatically? 我能否以字母顺序获取此信息? (I don't mean the poem, of course) (当然,我不是说这首诗)

If you have a DateTime object which represents a value in the month, then it's pretty straightforward. 如果你有一个表示月份值的DateTime对象,那么它非常简单。 You get the dayOfMonth property from that DateTime object and get the maximum value of the property. 您从该DateTime对象获取dayOfMonth属性并获取该属性的最大值。 Here is a sample function: 这是一个示例函数:

public static int daysOfMonth(int year, int month) {
  DateTime dateTime = new DateTime(year, month, 14, 12, 0, 0, 000);
  return dateTime.dayOfMonth().getMaximumValue();
}

Yes, although it's not as pretty as it might be: 是的,虽然它不像它可能那么漂亮:

import org.joda.time.*;
import org.joda.time.chrono.*;
import org.joda.time.field.*;

public class Test {
    public static void main(String[] args) {
        GregorianChronology calendar = GregorianChronology.getInstance();
        DateTimeField field = calendar.dayOfMonth();

        for (int i = 1; i < 12; i++) {
            LocalDate date = new LocalDate(2010, i, 1, calendar);
            System.out.println(field.getMaximumValue(date));
        }
    }
}

Note that I've hard-coded the assumption that there are 12 months, and that we're interested in 2010. I've explicitly selected the Gregorian chronology though - in other chronologies you'd get different answers, of course. 请注意,我已经对12个月的假设进行了硬编码,并且我们对2010年感兴趣。虽然我已经明确选择了格里高利年表 - 但在其他年表中,你会得到不同的答案。 (And the "12 month" loop wouldn't be a valid assumption either...) (并且“12个月”循环也不是一个有效的假设......)

I've gone for a LocalDate rather than a DateTime in order to fetch the value, to emphasize (however faintly :) that the value doesn't depend on the time zone. 我已经去了一个LocalDate而不是DateTime来获取值,以强调(但有点微弱:)该值不依赖于时区。

This is still not as simple as it looks, mind you. 请注意,这仍然不像它看起来那么简单。 I don't know off-hand what happens if use one chronology to construct the LocalDate , but ask for the maximum value of a field in a different chronology. 我不知道如果使用一个年表来构造LocalDate会发生什么,但是要求在不同的年代表中字段的最大值。 I have some ideas about what might happen, knowing a certain amount about Joda Time, but it's probably not a good idea :) 我对可能发生的事情有一些想法,知道一定数量的Joda Time,但这可能不是一个好主意:)

Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, yourMonth);
cal.set(Calendar.YEAR, yourYear);
cal.getActualMaximum(Calendar.DAY_OF_MONTH); // <-- the result!

Here is another simple function: 这是另一个简单的功能:

int daysOfMonth(DateTime dt) {
    int month = dt.getMonthOfYear();
    int month2 = month;
    int days = dt.getDay();
    DateTime dt2 = dt;
    while (month == month2 ) {
       days++;
       dt2.addDays(1);
       month2 = dt2.getMonthOfYear();
    }
    return (days - 1);
}

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

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