Reputation: 16050
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
Upvotes: 203
Views: 298721
Reputation: 2801
Absolutely zero hard-coding is needed.
MM
or YYYY
are accepted but optional%
) opfunction mon_year_2_num_days(__, ___, _) {
# __| MM
# ___| YYYY (opt; dflt := common year)
# |--> # days in month
return ((__ = +__) % (_ += _ ^= _<_) == (__++ < _^++_)) + _^_ \
+ (_ - __ ? _ : --__^(___ == "" || (___ = +___) % ++_ \
? !_ : ___ % (_ *= _ * ++_) || ___ % (__ * _ * __) == !_))
}
-1600 1 31 -800 1 31 0 1 31 800 1 31 1600 1 31 2400 1 31
-1600 2 29 -800 2 29 0 2 29 800 2 29 1600 2 29 2400 2 29
-1600 3 31 -800 3 31 0 3 31 800 3 31 1600 3 31 2400 3 31
-1600 4 30 -800 4 30 0 4 30 800 4 30 1600 4 30 2400 4 30
-1600 5 31 -800 5 31 0 5 31 800 5 31 1600 5 31 2400 5 31
-1600 6 30 -800 6 30 0 6 30 800 6 30 1600 6 30 2400 6 30
-1600 7 31 -800 7 31 0 7 31 800 7 31 1600 7 31 2400 7 31
-1600 8 31 -800 8 31 0 8 31 800 8 31 1600 8 31 2400 8 31
-1600 9 30 -800 9 30 0 9 30 800 9 30 1600 9 30 2400 9 30
-1600 10 31 -800 10 31 0 10 31 800 10 31 1600 10 31 2400 10 31
-1600 11 30 -800 11 30 0 11 30 800 11 30 1600 11 30 2400 11 30
-1600 12 31 -800 12 31 0 12 31 800 12 31 1600 12 31 2400 12 31
-1500 1 31 -700 1 31 100 1 31 900 1 31 1700 1 31 2500 1 31
-1500 2 28 -700 2 28 100 2 28 900 2 28 1700 2 28 2500 2 28
-1500 3 31 -700 3 31 100 3 31 900 3 31 1700 3 31 2500 3 31
-1500 4 30 -700 4 30 100 4 30 900 4 30 1700 4 30 2500 4 30
-1500 5 31 -700 5 31 100 5 31 900 5 31 1700 5 31 2500 5 31
-1500 6 30 -700 6 30 100 6 30 900 6 30 1700 6 30 2500 6 30
-1500 7 31 -700 7 31 100 7 31 900 7 31 1700 7 31 2500 7 31
-1500 8 31 -700 8 31 100 8 31 900 8 31 1700 8 31 2500 8 31
-1500 9 30 -700 9 30 100 9 30 900 9 30 1700 9 30 2500 9 30
-1500 10 31 -700 10 31 100 10 31 900 10 31 1700 10 31 2500 10 31
-1500 11 30 -700 11 30 100 11 30 900 11 30 1700 11 30 2500 11 30
-1500 12 31 -700 12 31 100 12 31 900 12 31 1700 12 31 2500 12 31
-1400 1 31 -600 1 31 200 1 31 1000 1 31 1800 1 31 2600 1 31
-1400 2 28 -600 2 28 200 2 28 1000 2 28 1800 2 28 2600 2 28
-1400 3 31 -600 3 31 200 3 31 1000 3 31 1800 3 31 2600 3 31
-1400 4 30 -600 4 30 200 4 30 1000 4 30 1800 4 30 2600 4 30
-1400 5 31 -600 5 31 200 5 31 1000 5 31 1800 5 31 2600 5 31
-1400 6 30 -600 6 30 200 6 30 1000 6 30 1800 6 30 2600 6 30
-1400 7 31 -600 7 31 200 7 31 1000 7 31 1800 7 31 2600 7 31
-1400 8 31 -600 8 31 200 8 31 1000 8 31 1800 8 31 2600 8 31
-1400 9 30 -600 9 30 200 9 30 1000 9 30 1800 9 30 2600 9 30
-1400 10 31 -600 10 31 200 10 31 1000 10 31 1800 10 31 2600 10 31
-1400 11 30 -600 11 30 200 11 30 1000 11 30 1800 11 30 2600 11 30
-1400 12 31 -600 12 31 200 12 31 1000 12 31 1800 12 31 2600 12 31
-1300 1 31 -500 1 31 300 1 31 1100 1 31 1900 1 31 2700 1 31
-1300 2 28 -500 2 28 300 2 28 1100 2 28 1900 2 28 2700 2 28
-1300 3 31 -500 3 31 300 3 31 1100 3 31 1900 3 31 2700 3 31
-1300 4 30 -500 4 30 300 4 30 1100 4 30 1900 4 30 2700 4 30
-1300 5 31 -500 5 31 300 5 31 1100 5 31 1900 5 31 2700 5 31
-1300 6 30 -500 6 30 300 6 30 1100 6 30 1900 6 30 2700 6 30
-1300 7 31 -500 7 31 300 7 31 1100 7 31 1900 7 31 2700 7 31
-1300 8 31 -500 8 31 300 8 31 1100 8 31 1900 8 31 2700 8 31
-1300 9 30 -500 9 30 300 9 30 1100 9 30 1900 9 30 2700 9 30
-1300 10 31 -500 10 31 300 10 31 1100 10 31 1900 10 31 2700 10 31
-1300 11 30 -500 11 30 300 11 30 1100 11 30 1900 11 30 2700 11 30
-1300 12 31 -500 12 31 300 12 31 1100 12 31 1900 12 31 2700 12 31
-1200 1 31 -400 1 31 400 1 31 1200 1 31 2000 1 31 2800 1 31
-1200 2 29 -400 2 29 400 2 29 1200 2 29 2000 2 29 2800 2 29
-1200 3 31 -400 3 31 400 3 31 1200 3 31 2000 3 31 2800 3 31
-1200 4 30 -400 4 30 400 4 30 1200 4 30 2000 4 30 2800 4 30
-1200 5 31 -400 5 31 400 5 31 1200 5 31 2000 5 31 2800 5 31
-1200 6 30 -400 6 30 400 6 30 1200 6 30 2000 6 30 2800 6 30
-1200 7 31 -400 7 31 400 7 31 1200 7 31 2000 7 31 2800 7 31
-1200 8 31 -400 8 31 400 8 31 1200 8 31 2000 8 31 2800 8 31
-1200 9 30 -400 9 30 400 9 30 1200 9 30 2000 9 30 2800 9 30
-1200 10 31 -400 10 31 400 10 31 1200 10 31 2000 10 31 2800 10 31
-1200 11 30 -400 11 30 400 11 30 1200 11 30 2000 11 30 2800 11 30
-1200 12 31 -400 12 31 400 12 31 1200 12 31 2000 12 31 2800 12 31
-1100 1 31 -300 1 31 500 1 31 1300 1 31 2100 1 31 2900 1 31
-1100 2 28 -300 2 28 500 2 28 1300 2 28 2100 2 28 2900 2 28
-1100 3 31 -300 3 31 500 3 31 1300 3 31 2100 3 31 2900 3 31
-1100 4 30 -300 4 30 500 4 30 1300 4 30 2100 4 30 2900 4 30
-1100 5 31 -300 5 31 500 5 31 1300 5 31 2100 5 31 2900 5 31
-1100 6 30 -300 6 30 500 6 30 1300 6 30 2100 6 30 2900 6 30
-1100 7 31 -300 7 31 500 7 31 1300 7 31 2100 7 31 2900 7 31
-1100 8 31 -300 8 31 500 8 31 1300 8 31 2100 8 31 2900 8 31
-1100 9 30 -300 9 30 500 9 30 1300 9 30 2100 9 30 2900 9 30
-1100 10 31 -300 10 31 500 10 31 1300 10 31 2100 10 31 2900 10 31
-1100 11 30 -300 11 30 500 11 30 1300 11 30 2100 11 30 2900 11 30
-1100 12 31 -300 12 31 500 12 31 1300 12 31 2100 12 31 2900 12 31
-1000 1 31 -200 1 31 600 1 31 1400 1 31 2200 1 31 3000 1 31
-1000 2 28 -200 2 28 600 2 28 1400 2 28 2200 2 28 3000 2 28
-1000 3 31 -200 3 31 600 3 31 1400 3 31 2200 3 31 3000 3 31
-1000 4 30 -200 4 30 600 4 30 1400 4 30 2200 4 30 3000 4 30
-1000 5 31 -200 5 31 600 5 31 1400 5 31 2200 5 31 3000 5 31
-1000 6 30 -200 6 30 600 6 30 1400 6 30 2200 6 30 3000 6 30
-1000 7 31 -200 7 31 600 7 31 1400 7 31 2200 7 31 3000 7 31
-1000 8 31 -200 8 31 600 8 31 1400 8 31 2200 8 31 3000 8 31
-1000 9 30 -200 9 30 600 9 30 1400 9 30 2200 9 30 3000 9 30
-1000 10 31 -200 10 31 600 10 31 1400 10 31 2200 10 31 3000 10 31
-1000 11 30 -200 11 30 600 11 30 1400 11 30 2200 11 30 3000 11 30
-1000 12 31 -200 12 31 600 12 31 1400 12 31 2200 12 31 3000 12 31
-900 1 31 -100 1 31 700 1 31 1500 1 31 2300 1 31 3100 1 31
-900 2 28 -100 2 28 700 2 28 1500 2 28 2300 2 28 3100 2 28
-900 3 31 -100 3 31 700 3 31 1500 3 31 2300 3 31 3100 3 31
-900 4 30 -100 4 30 700 4 30 1500 4 30 2300 4 30 3100 4 30
-900 5 31 -100 5 31 700 5 31 1500 5 31 2300 5 31 3100 5 31
-900 6 30 -100 6 30 700 6 30 1500 6 30 2300 6 30 3100 6 30
-900 7 31 -100 7 31 700 7 31 1500 7 31 2300 7 31 3100 7 31
-900 8 31 -100 8 31 700 8 31 1500 8 31 2300 8 31 3100 8 31
-900 9 30 -100 9 30 700 9 30 1500 9 30 2300 9 30 3100 9 30
-900 10 31 -100 10 31 700 10 31 1500 10 31 2300 10 31 3100 10 31
-900 11 30 -100 11 30 700 11 30 1500 11 30 2300 11 30 3100 11 30
-900 12 31 -100 12 31 700 12 31 1500 12 31 2300 12 31 3100 12 31
Upvotes: 0
Reputation: 3688
This is the mathematical way:
For year (e.g. 2012), month (1 to 12):
int daysInMonth = month !== 2 ?
31 - (((month - 1) % 7) % 2) :
28 + (year % 4 == 0 ? 1 : 0) - (year % 100 == 0 ? 1 : 0) + (year % 400 == 0 ? 1 : 0)
Upvotes: 14
Reputation: 103
if (month == 4 || month == 6 || month == 9 || month == 11) {
daysInMonth = 30;
} else if (month == 2) {
daysInMonth = (leapYear) ? 29 : 28;
else {
daysInMonth = 31;
}
Upvotes: 7
Reputation: 1
An alternative solution is to use a Calendar
object. Get the current date and set the day so it is the first of the month. Then add one month and take away one day to get the last day of the current month. Finally fetch the day to get the number of days in the month.
Calendar today = getInstance(TimeZone.getTimeZone("UTC"));
Calendar currMonthLastDay = getInstance(TimeZone.getTimeZone("UTC"));
currMonthLastDay.clear();
currMonthLastDay.set(YEAR, today.get(YEAR));
currMonthLastDay.set(MONTH, today.get(MONTH));
currMonthLastDay.set(DAY_OF_MONTH, 1);
currMonthLastDay.add(MONTH, 1);
currMonthLastDay.add(DAY_OF_MONTH, -1);
Integer daysInMonth = currMonthLastDay.get(DAY_OF_MONTH);
Upvotes: 0
Reputation: 77
Number of days in particular year - Java 8+ solution
Year.now().length()
Upvotes: -1
Reputation: 5474
An optimal and performant variance:
public static int daysInMonth(int month, int year) {
if (month != 2) {
return 31 - (month - 1) % 7 % 2;
}
else {
if ((year & 3) == 0 && ((year % 25) != 0 || (year & 15) == 0)) { // leap year
return 29;
} else {
return 28;
}
}
}
For more details on the leap algorithm check here
Upvotes: 0
Reputation: 759
String date = "11-02-2000";
String[] input = date.split("-");
int day = Integer.valueOf(input[0]);
int month = Integer.valueOf(input[1]);
int year = Integer.valueOf(input[2]);
Calendar cal=Calendar.getInstance();
cal.set(Calendar.YEAR,year);
cal.set(Calendar.MONTH,month-1);
cal.set(Calendar.DATE, day);
//since month number starts from 0 (i.e jan 0, feb 1),
//we are subtracting original month by 1
int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println(days);
Upvotes: -1
Reputation: 219
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));
}
Upvotes: 1
Reputation: 1343
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;
Upvotes: 0
Reputation: 877
I would go for a solution like this:
int monthNr = getMonth();
final Month monthEnum = Month.of(monthNr);
int daysInMonth;
if (monthNr == 2) {
int year = getYear();
final boolean leapYear = IsoChronology.INSTANCE.isLeapYear(year);
daysInMonth = monthEnum.length(leapYear);
} else {
daysInMonth = monthEnum.maxLength();
}
If the month isn't February (92% of the cases), it depends on the month only and it is more efficient not to involve the year. This way, you don't have to call logic to know whether it is a leap year and you don't need to get the year in 92% of the cases. And it is still clean and very readable code.
Upvotes: 4
Reputation: 21
// 1 means Sunday ,2 means Monday .... 7 means Saturday
//month starts with 0 (January)
MonthDisplayHelper monthDisplayHelper = new MonthDisplayHelper(2019,4);
int numbeOfDaysInMonth = monthDisplayHelper.getNumberOfDaysInMonth();
Upvotes: 2
Reputation: 1
This worked fine for me.
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.
}
} //[email protected]
Upvotes: -1
Reputation: 139
Lets make it as simple if you don't want to hardcode the value of year and month and you want to take the value from current date and time:
Date d = new Date();
String myDate = new SimpleDateFormat("dd/MM/yyyy").format(d);
int iDayFromDate = Integer.parseInt(myDate.substring(0, 2));
int iMonthFromDate = Integer.parseInt(myDate.substring(3, 5));
int iYearfromDate = Integer.parseInt(myDate.substring(6, 10));
YearMonth CurrentYear = YearMonth.of(iYearfromDate, iMonthFromDate);
int lengthOfCurrentMonth = CurrentYear.lengthOfMonth();
System.out.println("Total number of days in current month is " + lengthOfCurrentMonth );
Upvotes: 2
Reputation: 380
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;
}
}
Upvotes: -1
Reputation: 1551
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;
}
Upvotes: 3
Reputation: 30648
@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
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
Upvotes: 459
Reputation: 28279
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);
Upvotes: 0
Reputation: 1
public class Main {
private static LocalDate local=LocalDate.now();
public static void main(String[] args) {
int month=local.lengthOfMonth();
System.out.println(month);
}
}
Upvotes: 0
Reputation: 63
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.
Upvotes: 2
Reputation: 5631
java.time.LocalDate
From Java 1.8, you can use the method lengthOfMonth
on java.time.LocalDate
:
LocalDate date = LocalDate.of(2010, 1, 19);
int days = date.lengthOfMonth();
Upvotes: 30
Reputation: 1500535
If you have to use java.util.Calendar
, I suspect you want:
int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
Personally, however, I'd suggest using Joda Time instead of java.util.{Calendar, Date}
to start with, in which case you could use:
int days = chronology.dayOfMonth().getMaximumValue(date);
Note that rather than parsing the string values individually, it would be better to get whichever date/time API you're using to parse it. In java.util.*
you might use SimpleDateFormat
; in Joda Time you'd use a DateTimeFormatter
.
Upvotes: 52
Reputation: 1
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);
}
}
Upvotes: 0
Reputation: 57316
You can use Calendar.getActualMaximum
method:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
int numDays = calendar.getActualMaximum(Calendar.DATE);
Upvotes: 33