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:02
if (month == 4 || month == 6 || month == 9 || month == 11)

daysInMonth = 30;

else 

if (month == 2) 

daysInMonth = (leapYear) ? 29 : 28;

else 

daysInMonth = 31;
查看更多
牵手、夕阳
3楼-- · 2019-01-01 13:03

This worked fine for me.

This is a Sample Output

import java.util.*;

public class DaysInMonth{ 

public static void main(String args []){ 

Scanner input = new Scanner(System.in); 
System.out.print("Enter a year:"); 

int year = input.nextInt(); //Moved here to get input after the question is asked 

System.out.print("Enter a month:"); 
int month = input.nextInt(); //Moved here to get input after the question is asked 

int days = 0; //changed so that it just initializes the variable to zero
boolean isLeapYear = (year % 4 == 0 && year % 100 != 0)||(year % 400 == 0); 

switch (month){ 
case 1: 
days = 31; 
break; 
case 2: 
if (isLeapYear) 
days = 29; 
else 
days = 28; 
break; 
case 3: 
days = 31; 
break; 
case 4: 
days = 30; 
break; 
case 5: 
days = 31; 
break; 
case 6: 
days = 30; 
break; 
case 7: 
days = 31; 
break; 
case 8: 
days = 31; 
break; 
case 9: 
days = 30; 
break; 
case 10: 
days = 31; 
break; 
case 11: 
days = 30; 
break; 
case 12: 
days = 31; 
break; 
default: 
String response = "Have a Look at what you've done and try again";
System.out.println(response); 
System.exit(0); 
} 
String response = "There are " +days+ " Days in Month "+month+ " of Year " +year+ ".\n"; 
System.out.println(response); // new line to show the result to the screen. 
} 
} //abhinavsthakur00@gmail.com
查看更多
怪性笑人.
4楼-- · 2019-01-01 13:03
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/*
 * 44. Return the number of days in a month
 * , where month and year are given as input.
 */
public class ex44 {
    public static void dateReturn(int m,int y)
    {
        int m1=m;
        int y1=y;
        String str=" "+ m1+"-"+y1;
        System.out.println(str);
        SimpleDateFormat sd=new SimpleDateFormat("MM-yyyy");

        try {
            Date d=sd.parse(str);
            System.out.println(d);
            Calendar c=Calendar.getInstance();
            c.setTime(d);
            System.out.println(c.getActualMaximum(Calendar.DAY_OF_MONTH));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    public static void main(String[] args) {
dateReturn(2,2012);


    }

}
查看更多
无与为乐者.
5楼-- · 2019-01-01 13:03

Following method will provide you the no of days in a particular month

public static int getNoOfDaysInAMonth(String date) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return (cal.getActualMaximum(Calendar.DATE));
    }
查看更多
美炸的是我
6楼-- · 2019-01-01 13:04
String  MonthOfName = "";
int number_Of_DaysInMonth = 0;

//year,month
numberOfMonth(2018,11); // calling this method to assign values to the variables MonthOfName and number_Of_DaysInMonth 

System.out.print("Number Of Days: "+number_Of_DaysInMonth+"   name of the month: "+  MonthOfName );

public void numberOfMonth(int year, int month) {
    switch (month) {
        case 1:
            MonthOfName = "January";
            number_Of_DaysInMonth = 31;
            break;
        case 2:
            MonthOfName = "February";
            if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
                number_Of_DaysInMonth = 29;
            } else {
                number_Of_DaysInMonth = 28;
            }
            break;
        case 3:
            MonthOfName = "March";
            number_Of_DaysInMonth = 31;
            break;
        case 4:
            MonthOfName = "April";
            number_Of_DaysInMonth = 30;
            break;
        case 5:
            MonthOfName = "May";
            number_Of_DaysInMonth = 31;
            break;
        case 6:
            MonthOfName = "June";
            number_Of_DaysInMonth = 30;
            break;
        case 7:
            MonthOfName = "July";
            number_Of_DaysInMonth = 31;
            break;
        case 8:
            MonthOfName = "August";
            number_Of_DaysInMonth = 31;
            break;
        case 9:
            MonthOfName = "September";
            number_Of_DaysInMonth = 30;
            break;
        case 10:
            MonthOfName = "October";
            number_Of_DaysInMonth = 31;
            break;
        case 11:
            MonthOfName = "November";
            number_Of_DaysInMonth = 30;
            break;
        case 12:
            MonthOfName = "December";
            number_Of_DaysInMonth = 31;
    }
}
查看更多
宁负流年不负卿
7楼-- · 2019-01-01 13:06

This is the mathematical way:

For year, month (1 to 12):

int daysInMonth = month == 2 ? 
    28 + (year % 4 == 0 ? 1:0) - (year % 100 == 0 ? (year % 400 == 0 ? 0 : 1) : 0) :
    31 - (month-1) % 7 % 2;
查看更多
登录 后发表回答