## Counting number of days of a particular month when given a duration

number of days between two dates excel

number of months between two dates - excel

excel count number of days in month between two dates

how to calculate the number of days in a month

excel function to count number of days in month

excel days passed in month

days left in month excel

A duration is given. Ex: Jan 15-March 15

I want to count the number of days which belongs to each month, in that given duration. In this example, number of days of January in that duration; 15 number of days of February in that duration; 28 number of days of March in that duration; 15

I'm looking for a solution other that traversing through each date of the duration and checking if `Date.getMonth() = "Month I want to check against"`

Is there an easier way of doing this using methods in Java Date or Java SQL Date or using any other Date type?

`Map < YearMonth , Long >`

with lambda syntax

Here is a solution using a bit of terse code using streams and lambdas. While this solution does traverse each date of the time range, the simplicity and clarity of the code may outweigh that inefficiency.

Use `LocalDate`

for the starting and stopping date. Use `YearMonth`

to track each month.

LocalDate start = LocalDate.of( 2019 , 1 , 15 ); LocalDate stop = LocalDate.of( 2019 , 3 , 16 );

Make a `Map`

to keep a number of days for each month.

Map < YearMonth, Long > map = start .datesUntil( stop ) .collect( Collectors.groupingBy( ( LocalDate localDate ) -> YearMonth.from( localDate ) , TreeMap::new , Collectors.counting() ) );

Dump to console.

{2019-01=17, 2019-02=28, 2019-03=15}

System.out.println( map );

Given a starting date,

`LocalDate::datesUntil`

provides a`Stream`

of`LocalDate`

objects, incremented by`days`

.Then just do a grouping into a

`SortedMap`

(a`TreeMap`

) to keep months in chronological order, classified by the`YearMonth`

and counting the days for that month in the range.

If you want the total days you can just do

long totalDays = d.datesUntil(LocalDate.of(2019, 3, 16)).count();

**Date Duration Calculator: Days Between Dates,** How many days, months, and years are there between two dates? Count Days Add DaysWorkdaysAdd WorkdaysWeekdayWeek №. Start Date. Month: Duration Between Two Dates – Calculates number of days. Date Calculator – Add or subtract days, months, years Birthday Calculator – Find when you are 1 billion seconds old

This is just a simple example I threw together with some basic research.

LocalDate from = LocalDate.of(2019, Month.JANUARY, 15); LocalDate to = LocalDate.of(2019, Month.MARCH, 15); DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("MMM"); LocalDate date = from; while (date.isBefore(to)) { LocalDate endOfMonth = date.withDayOfMonth(date.lengthOfMonth()); if (endOfMonth.isAfter(to)) { endOfMonth = to; } // Inclusive to exclusive comparison long days = ChronoUnit.DAYS.between(date, endOfMonth.plusDays(1)); System.out.println(days + " days in " + date.format(monthFormatter)); date = date.plusMonths(1).withDayOfMonth(1); }

This will output

17 days in Jan. 28 days in Feb. 15 days in Mar.

There are probably better ways to achieve the same result, but as I said, I just threw it together with a little bit of Googling and trial and error.

As has already been stated, you should avoid using the older, out-of-date and effectively deprecated `Date`

, `Calendar`

and associated classes.

**Excel Formula,** Cell "A2" displays the current date and time. Custom Cell "A5" displays the number of days in the current month of the current year using EOMONTH. DATE - The date as a date serial number given a year, month, day. Duration Between Two Dates – Calculates number of days. Time and Date Duration – Calculate duration, with both date and time included Birthday Calculator – Find when you are 1 billion seconds old

Try this. May be something like this you want. So it set a startdate and enddate, then loop for each moth till the end date and calculate the day count. I have not tested it thoroughly, but should be close to your concept.

public static void main(String[] args) throws ParseException { String startDateS = "01/15/2019"; String endDateS = "03/15/2019"; SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); Date startDate = dateFormat.parse(startDateS); Date endDate = dateFormat.parse(endDateS); while (endDate.compareTo(startDate) > 0) { Calendar c = Calendar.getInstance(); c.setTime(startDate); c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH)); Date endOfMonth = c.getTime(); if( endDate.compareTo(endOfMonth) > 0 ) System.out.println("Count Month " + getMonthForInt(c.get(Calendar.MONTH)) + " " + getDifferenceDays(startDate, endOfMonth)); else System.out.println("Count Month " + getMonthForInt(c.get(Calendar.MONTH)) + " " + getDifferenceDays(startDate, endDate)); c.add(Calendar.DAY_OF_MONTH, 1); startDate = c.getTime(); } } static String getMonthForInt(int num) { String month = "wrong"; DateFormatSymbols dfs = new DateFormatSymbols(); String[] months = dfs.getMonths(); if (num >= 0 && num <= 11) { month = months[num]; } return month; } public static int getDifferenceDays(Date d1, Date d2) { int daysdiff = 0; long diff = d2.getTime() - d1.getTime(); long diffDays = diff / (24 * 60 * 60 * 1000) + 1; daysdiff = (int) diffDays; return daysdiff; }

You can do the same using Java.time in Java 8.

public static void main(String[] args) throws ParseException { String startDateS = "01/15/2019"; String endDateS = "03/15/2019"; DateTimeFormatter format1 = DateTimeFormatter.ofPattern("MM/dd/yyyy"); LocalDate startDate = LocalDate.parse(startDateS, format1); LocalDate endDate = LocalDate.parse(endDateS, format1); while (endDate.compareTo(startDate) > 0) { LocalDate endOfMonth = startDate.minusDays(startDate.getDayOfMonth()).plusMonths(1); if( endDate.compareTo(endOfMonth) > 0 ) System.out.println("Count Month " + getMonthForInt(startDate) + " " + getDifferenceDays(startDate, endOfMonth)); else System.out.println("Count Month " + getMonthForInt(startDate) + " " + getDifferenceDays(startDate, endDate)); startDate = endOfMonth.plusDays(1); } } static String getMonthForInt(LocalDate startDate) { return startDate.getMonth().getDisplayName( TextStyle.FULL , Locale.US ); } public static long getDifferenceDays(LocalDate d1, LocalDate d2) { // return Duration.between(d2.atStartOfDay(), d1.atStartOfDay()).toDays(); return ChronoUnit.DAYS.between(d1, d2) + 1; }

**How to Count Days in Month in Excel,** This formula can calculate number of days in given date's month in before you can blink. Calculate Minutes Between Dates & Time In Microsoft Excel A duration is given. Ex: Jan 15-March 15 I want to count the number of days which belongs to each month, in that given duration. In this example, number of days of January in that duration; 15 n

**Calculating Number of Days, Months and Years between Dates in ,** You can count the number of days, months and years between the two given dates separately with How to Convert Date and Time from GMT to CST in Excel . Duration Between Two Dates – Calculates number of days. Time and Date Duration – Calculate duration, with both date and time included; Date Calculator – Add or subtract days, months, years; Birthday Calculator – Find when you are 1 billion seconds old; Related Links. Date/calendar related services – Overview

**How to calculate number of days in a month or a year in Excel?,** Calculate number of days in a given month with formulas Save 50% of your time, and reduce thousands of mouse clicks for you every day! handle down to the cells that you want to calculate the days in the specific month, see screenshot: . The DATEDIF function calculates the period or the difference between two dates in days, months, and years. You can use the DATEDIF function to determine the time frame for an upcoming project, or it can be used, along with a person's birth date, to calculate an individual's age in years, months, and days, for example.

**Excel formula: Days in month,** To calculate the last day of a month based on a given date, you can use the EOMONTH function. In the example shown, the formula in cell B5 is: = EOMONTH ( B5� Use the Excel DAYS360 Function in accounting systems to calculate the number of days between two dates based on a 360-day year. Use this function to help compute payments if your accounting system is based on 12 30-day months.

##### Comments

- SQL appears irrelevant in this context
- is it a string which is given and you need to count the number of days? Do you need number of days for each month? Year is not present in the String?
- Is start date inclusive? What about end date? In the example, do you want to count 14 or 15 days of Match? And 28 or 29 days of February? Can we tell the year so we can determine whether it’s a leap year?
- Yes, it’s possible, but the question seems very broad and poorly researched. Use
`LocalDate`

and`ChronoUnit.DAYS.between()`

. Don’t use`java.util.Date`

. That class is poorly designed and long outdated and despite the name does not represent a date. - (1-) What does this have to do with Swing?
- Impressive. It is
*traversing through each date of the duration*, which the questioner wanted to avoid, but given the terseness it might be worth it in this case. - Clever solution. I changed your code to use
`YearMonth`

rather than`Month`

, in case the date range went over 12 months. - Good idea. I didn't really have time to think about this until I posted the answer, having looked up
`localDate`

. Notice that I handled the`exclusive`

part of`datesUntil`

rather clumsily which also should be refined. Perhaps by adding a day using`plus()`

- As an option, the second argument to the
`groupingBy`

could be`LinkedHashMap::new`

to allow the dates to be displayed in chonological order. - @WJS If you mean
`datesUntil`

running up to, but not including, the limit of March 16th, that is a feature, not a bug. The Half-Open approach is commonly used for date-time handling and generally the wisest way to go. - And when the question asked for 15 days in January, that must be a bug in the question, not in the answer. Jan 15 through 31 inclusive makes 17 days.
- @OleV.V. Honestly, I couldn't really make heads or tails of that part
- There weren’t any. :-)
- Please don’t teach the young ones to use the long outdated and notoriously troublesome
`SimpleDateFormat`

class. At least not as the first option. And not without any reservation. Today we have so much better in`java.time`

, the modern Java date and time API, and its`DateTimeFormatter`

.