如何使用Java日历获取特定月份的天数?

问题描述 投票:0回答:4

我正在创建一个简单的程序,允许用户查看他们设置的月份之间有多少天。

例如从一月到三月

我可以使用这个获取当月的当前日期:

Calendar.DAY_OF_MONTH

我想要的是如何提供当月的值?

我有这个简单的代码:

public static void machineProblemTwo(int startMonth, int endMonth) {

        int[] month = new int[0];
        int date = 2015;

        for(int x = startMonth; x <= endMonth; x++) {
           System.out.println(x + " : " + getMaxDaysInMonth(x,date));
        }

    }

public static int getMaxDaysInMonth(int month, int year){

        Calendar cal = Calendar.getInstance();
        int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH); // how can I supply the month here?

        return days;

    }

我该怎么做?你能帮我解决这个问题吗?

java calendar
4个回答
8
投票

在询问最大值之前,您需要将日历设置为当年和月份的be,例如与

cal.set(year, month, 1);

(或者按照大卫的回答使用日历构造函数。)

所以:

public static int getMaxDaysInMonth(int month, int year) {
    Calendar cal = Calendar.getInstance();
    // Note: 0-based months
    cal.set(year, month, 1);
    return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
}

或者 - 最好 - 使用 Joda Time 或 Java 8:

// Java 8: 1-based months
return new LocalDate(year, month, 1).lengthOfMonth();

// Joda Time: 1-based months
return new LocalDate(year, month, 1).dayOfMonth().getMaximumValue();

(我确信Joda Time一定有一个更简单的选择,但我还没有找到......)


6
投票

使用

GregorianCalendar
的构造函数来传递年、月和日。 不要忘记月份是从 0 到 11(0 代表一月,11 代表十二月)。

public static int numberOfDaysInMonth(int month, int year) {
    Calendar monthStart = new GregorianCalendar(year, month, 1);
    return monthStart.getActualMaximum(Calendar.DAY_OF_MONTH);
}

0
投票

我们也可以这样做:

private DateTime getNewDate(DateTime date, int dayOfMonth) {
    // first we need to make it 1st day of the month
    DateTime newDateTime = new DateTime(date.getYear(), date.getMonthOfYear(), 1, 0, 0);
    int maximumValueOfDays = newDateTime.dayOfMonth().getMaximumValue();

    // to handle the month which has less than 31 days
    if (dayOfMonth > maximumValueOfDays) {
      newDateTime = newDateTime.dayOfMonth().withMaximumValue();
    } else {
      newDateTime = newDateTime.withDayOfMonth(dayOfMonth);
    }
    return newDateTime;
  }

0
投票

java.time

当然,二月的长度各不相同。所以我们需要一年。

int year = 2025 ; 

与遗留的日期时间类相比,java.time类使用合理的编号。因此,1 月至 12 月的月份编号为 1-12。

int startMonthNumber = 1 , endMonthNumber = 3 ; 

使用这些输入来获取开始和结束

YearMonth
对象。

final YearMonth start = YearMonth.of( year , startMonthNumber );
final YearMonth end = YearMonth.of( year , endMonthNumber );

遍历该跨度内的所有月份。获取每个的长度,然后相加。

YearMonth yearMonth = start;
int countDays = 0;
while ( !yearMonth.isAfter( end ) )
{
    countDays += yearMonth.lengthOfMonth( );
    yearMonth = yearMonth.plusMonths( 1 );
}
System.out.println( "Days across " + start + "/" + end + " = " + countDays );

另一种方法是使用一系列

LocalDate
对象,一个对象代表跨度内每月的每一天。要求流计算计数。我们可以用一行代码来完成此操作。

long count =
        start
                .atDay( 1 )
                .datesUntil(
                        end
                                .plusMonths( 1 )
                                .atDay( 1 )
                )
                .count( );
© www.soinside.com 2019 - 2024. All rights reserved.