Number of days in particular month of particular year?
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
Determine how many days in a given month
LengthOfMonth
method is for that. Besides, I think Calendar
API shouldn't be used anymore. It is already old.
LocalDate date = LocalDate.of(2019, 6, 20);
int days = date.lengthOfMonth();
5 Reasons Why Java's old Date and Calendar API was Bad
Number Of Days in a month of particular year Not Working
Wasn't there 31 days in March 2000? Month is zero based, as the doc say (http://docs.oracle.com/javase/8/docs/api/java/util/GregorianCalendar.html#GregorianCalendar-int-int-int-)
Edit: I'm stupid, I thought you had the number of days in a month figured out. You need to use getActualMaximum, not get. (http://docs.oracle.com/javase/8/docs/api/java/util/GregorianCalendar.html#getActualMaximum-int-)
Getting number of days for a specific month and year between two dates SQL
The generic formula for the number of overlapping days in two ranges is
MAX(MIN(end1, end2) - MAX(start1, start2) + 1, 0)
In your case you have one set of Start and End dates, you must construct the other from the given month and year using datefromparts
and eomonth
.
Unfortunately SQL Server doesn't support LEAST
and GREATEST
formulas as do MySQL and Oracle, so this is a bit painful to implement. Here's an example using variables:
declare @month int;
declare @year int;
declare @startDate date;
declare @endDate date;
declare @startOfMonth date;
declare @endOfMonth date;
declare @minEnd date;
declare @maxStart date;
set @month = 1;
set @year = 2020;
set @startDate = '2019-11-12';
set @endDate = '2020-01-13';
set @startOfMonth = datefromparts(@year, @month, 1)
set @endOfMonth = eomonth(@startOfMonth)
set @minEnd = case when @endDate < @endOfMonth then @endDate
else @endOfMonth
end;
set @maxStart = case when @startDate < @startOfMonth then @startOfMonth
else @startDate
end;
select case when datediff(day, @maxStart, @minEnd) + 1 < 0 then 0
else datediff(day, @maxStart, @minEnd) + 1
end as days_in_month
Output:
13
Demo on dbfiddle; this includes other sample date ranges.
You could implement something similar using a series of CTEs if the values are derived from a table.
excel calculate number of days per month in certain period
I am just posting another IF based variation which I had worked out.
=IF(OR(AND(C$1>$A2,$B2>C$1),AND(EOMONTH(C$1,0)>$A2,$B2>EOMONTH(C$1,0))),MIN($B2,EOMONTH(C$1,0))-MAX($A2,C$1)+1,0)
Primary condition is to check whether any date mentioned on row 1 (within the month) is intersecting the either start or end date specified in cell A2
and B2
. Once this is cleared then the primary logic which you had posted works just fine.
You will have to change the argument separators as per your locale!
Edit-
@AnilGoyal spotted the formula error and I am posting the revised formula. I have put those missing AND
conditions in OR
. In addition to that the formula would have missed a boundary condition where date was first or last day of the month.
=IF(OR(AND(C$1>=$A2,C$1<=$B2),AND(EOMONTH(C$1,0)>=$A2,EOMONTH(C$1,0)<=$B2),AND($A2<=EOMONTH(C$1,0),$A2>=C$1),AND($B2<=EOMONTH(C$1,0),$B2>=C$1)),MIN($B2,EOMONTH(C$1,0))-MAX($A2,C$1)+1,0)
Overall, OP's original formula shall work well. As it turns out below formula seems to work just fine as well.=MAX(0,MIN($B2,EOMONTH(E$1,0))-MAX($A2,E$1)+1)
How to get all the dates and name of days of a particular month of an year in PHP?
Try:
$number_of_days = cal_days_in_month(CAL_GREGORIAN,$month, $year);
for ($x = 1; $x <= $number_of_days; $x++) {
echo date("F", strtotime($x . "-" . $month . "-" . $year)) . " " . $x . "," . $year . "(" . date("l", strtotime($x . "-" . $month . "-" . $year)) . ")<br/>";
}
output:
July 1,2016(Friday)
July 2,2016(Saturday)
July 3,2016(Sunday)
July 4,2016(Monday)
July 5,2016(Tuesday)
July 6,2016(Wednesday)
July 7,2016(Thursday)
July 8,2016(Friday)
July 9,2016(Saturday)
July 10,2016(Sunday)
July 11,2016(Monday)
July 12,2016(Tuesday)
July 13,2016(Wednesday)
July 14,2016(Thursday)
July 15,2016(Friday)
July 16,2016(Saturday)
July 17,2016(Sunday)
July 18,2016(Monday)
July 19,2016(Tuesday)
July 20,2016(Wednesday)
July 21,2016(Thursday)
July 22,2016(Friday)
July 23,2016(Saturday)
July 24,2016(Sunday)
July 25,2016(Monday)
July 26,2016(Tuesday)
July 27,2016(Wednesday)
July 28,2016(Thursday)
July 29,2016(Friday)
July 30,2016(Saturday)
July 31,2016(Sunday)
Number of days of a week inside a month in SQL
This is sort of a SQL Server setting (or trick) which works because the 1st of January, 1900 was a Monday. Since that's where SQL Server starts counting from it makes it easier to locate the first Thursday of any month. Thanks to Jeff Moden btw. I got this from something he wrote. Maybe there's a better way to this now, idk
with iso_dts_cte(yr, mo, wk) as (
select * from (values ('2020', '12', '50'),
('2020', '12', '51'),
('2020', '12', '52'),
('2020', '12', '53'),
('2021', '01', '01'),
('2021', '01', '02'),
('2021', '01', '03')) v(yr, mo, wk))
select iso.*, v.*
from iso_dts_cte iso
cross apply (values (cast(dateadd(wk,datediff(wk,0,'01/04/'+iso.yr),0)+((iso.wk-1)*7) as date),
cast(dateadd(wk,datediff(wk,0,'01/04/'+iso.yr),0)+((iso.wk)*7)-1 as date))) v(start_dt, end_dt);
yr mo wk start_dt end_dt
2020 12 50 2020-12-07 2020-12-13
2020 12 51 2020-12-14 2020-12-20
2020 12 52 2020-12-21 2020-12-27
2020 12 53 2020-12-28 2021-01-03
2021 01 01 2021-01-04 2021-01-10
2021 01 02 2021-01-11 2021-01-17
2021 01 03 2021-01-18 2021-01-24
To expand the week ranges into days and then count by calendar year and calendar month you could try something like this.
[Edit] It's my understanding the date hierarchy you're looking for is 1) calendar year, 2) calendar month, 3) iso week. The output seems to match the example now. However, there's not a way to ORDER BY
to display like the example.
with
iso_dts_cte(yr, mo, wk) as (
select * from (values ('2020', '12', '50'),
('2020', '12', '51'),
('2020', '12', '52'),
('2020', '12', '53'),
('2021', '01', '01'),
('2021', '01', '02'),
('2021', '01', '03')) v(yr, mo, wk)),
days_cte(n) as (
select * from (values (1),(2),(3),(4),(5),(6),(7)) v(n))
select year(dt.calc_dt) cal_yr, month(dt.calc_dt) cal_mo, iso.wk, count(*) day_count
from iso_dts_cte iso
cross apply (values (cast(dateadd(wk,datediff(wk,0,'01/04/'+iso.yr),0)+((iso.wk-1)*7) as date),
cast(dateadd(wk,datediff(wk,0,'01/04/'+iso.yr),0)+((iso.wk)*7)-1 as date))) v(start_dt, end_dt)
cross join days_cte d
cross apply (values (dateadd(day, d.n-1, v.start_dt))) dt(calc_dt)
group by year(dt.calc_dt), month(dt.calc_dt), iso.wk;
cal_yr cal_mo wk day_count
2020 12 50 7
2020 12 51 7
2020 12 52 7
2020 12 53 4
2021 1 01 7
2021 1 02 7
2021 1 03 7
2021 1 53 3
determine maximum number of days in particular month
You could use the DaysInMonth
method on DateTime
:
Date2 = new DateTime(year, month, DateTime.DaysInMonth(year, month));
Related Topics
Linux Command for Extracting War File
How to Debug Java -Jni Using Gdb on Linux
How to Get Rjava 0.9-3 to Work on Os X 10.7.4 with Oracle Java 1.7
Ruby and Duck Typing: Design by Contract Impossible
Jsp, Can It Work Similar to Yield, Layout, Content_For in Ruby/Rails/Erb
Running Java Program from Command Line Linux
How to Troubleshoot "Inconsistency Detected: Dl-Lookup.C: 111" (Java Result 127) Error
Javafx Freeze on Desktop.Open(File), Desktop.Browse(Uri)
How to View/Change Socket Connection Timeout on Linux
Can't Make Jdbc Connection to MySQL (Using Java, Intellij, and Linux)
Inputstream.Available() Is 0 Always
How to Achieve Javafx Mouse Event "Push and Hold"
Installing Oracle Jdk on Windows Subsystem for Linux