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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3772945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 04:44:57  来源:igfitidea点击:

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

javadatetimejodatime

提问by Manuel Aráoz

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)

我可以按字谜方式获取此信息吗?(当然,我不是指那首诗)

采纳答案by Erick Robertson

If you have a DateTimeobject which represents a value in the month, then it's pretty straightforward. You get the dayOfMonthproperty from that DateTimeobject and get the maximum value of the property. Here is a sample function:

如果您有一个DateTime表示月份中的值的对象,那么它非常简单。您dayOfMonth从该DateTime对象获取属性并获得该属性的最大值。这是一个示例函数:

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

回答by Benoit Courtine

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

回答by Jon Skeet

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 explicitlyselected the Gregorian chronology though - in other chronologies you'd get different answers, of course. (And the "12 month" loop wouldn't be a valid assumption either...)

请注意,我已经硬编码了 12 个月的假设,并且我们对 2010 年感兴趣。不过,我已经明确选择了公历年表 - 当然,在其他年表中,您会得到不同的答案。(而且“12 个月”循环也不是一个有效的假设......)

I've gone for a LocalDaterather than a DateTimein order to fetch the value, to emphasize (however faintly :) that the value doesn't depend on the time zone.

我已经使用 aLocalDate而不是 aDateTime来获取值,以强调(但微弱:)该值不依赖于时区。

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. I have some ideas about what mighthappen, knowing a certain amount about Joda Time, but it's probably not a good idea :)

请注意,这仍然不像看起来那么简单。如果使用一个年表来构造LocalDate,我不知道会发生什么,但是在不同的年表中要求字段的最大值。我对可能发生的事情有一些想法,对 Joda Time 有一定的了解,但这可能不是一个好主意:)

回答by David.Chu.ca

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