Number of days in particular month of particular y

2019-01-01 12:54发布

How to know how many days has particular month of particular year?

String date = "2010-01-19";
String[] ymd = date.split("-");
int year = Integer.parseInt(ymd[0]);
int month = Integer.parseInt(ymd[1]);
int day = Integer.parseInt(ymd[2]);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,month);
int daysQty = calendar.getDaysNumber(); // Something like this

16条回答
查无此人
2楼-- · 2019-01-01 13:08

Simple as that,no need to import anything

public static int getMonthDays(int month, int year) {
    int daysInMonth ;
    if (month == 4 || month == 6 || month == 9 || month == 11) {
        daysInMonth = 30;
    }
    else {
        if (month == 2) {
            daysInMonth = (year % 4 == 0) ? 29 : 28;
        } else {
            daysInMonth = 31;
        }
    }
    return daysInMonth;
}
查看更多
牵手、夕阳
3楼-- · 2019-01-01 13:08

Java 8 and later

@Warren M. Nocos. If you are trying to use Java 8's new Date and Time API, you can use java.time.YearMonth class. See Oracle Tutorial.

// Get the number of days in that month
YearMonth yearMonthObject = YearMonth.of(1999, 2);
int daysInMonth = yearMonthObject.lengthOfMonth(); //28  

Test: try a month in a leap year:

yearMonthObject = YearMonth.of(2000, 2);
daysInMonth = yearMonthObject.lengthOfMonth(); //29 

Java 7 and earlier

Create a calendar, set year and month and use getActualMaximum

int iYear = 1999;
int iMonth = Calendar.FEBRUARY; // 1 (months begin with 0)
int iDay = 1;

// Create a calendar object and set year and month
Calendar mycal = new GregorianCalendar(iYear, iMonth, iDay);

// Get the number of days in that month
int daysInMonth = mycal.getActualMaximum(Calendar.DAY_OF_MONTH); // 28

Test: try a month in a leap year:

mycal = new GregorianCalendar(2000, Calendar.FEBRUARY, 1);
daysInMonth= mycal.getActualMaximum(Calendar.DAY_OF_MONTH);      // 29
查看更多
骚的不知所云
4楼-- · 2019-01-01 13:12

You can use Calendar.getActualMaximum method:

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONTH, month-1);
    int numDays = calendar.getActualMaximum(Calendar.DATE);

And month-1 is Because of month takes its original number of month while in method takes argument as below in Calendar.class

   public int getActualMaximum(int field) {
      throw new RuntimeException("Stub!");
   }

And the (int field) is like as below.

    public static final int JANUARY = 0;
    public static final int NOVEMBER = 10;
    public static final int DECEMBER = 11;
查看更多
不再属于我。
5楼-- · 2019-01-01 13:12
public class Main {

    private static LocalDate local=LocalDate.now();
    public static void main(String[] args) {

            int month=local.lengthOfMonth();
            System.out.println(month);

    }
}
查看更多
永恒的永恒
6楼-- · 2019-01-01 13:13

The use of outdated Calendar API should be avoided.

In Java8 or higher version, this can be done with YearMonth.

Example code:

int year = 2011;
int month = 2;
YearMonth yearMonth = YearMonth.of(year, month);
int lengthOfMonth = yearMonth.lengthOfMonth();
System.out.println(lengthOfMonth);
查看更多
何处买醉
7楼-- · 2019-01-01 13:16

In Java8 you can use get ValueRange from a field of a date.

LocalDateTime dateTime = LocalDateTime.now();

ChronoField chronoField = ChronoField.MONTH_OF_YEAR;
long max = dateTime.range(chronoField).getMaximum();

This allows you to parameterize on the field.

查看更多
登录 后发表回答