YearMonth
Apparently you want to track year-month values. There's a class for that, named, well, YearMonth
. Find this class in the java.time package that supplants the terribly troublesome old date-time classes bundled with the earliest versions of Java.
MonthDay
Or maybe you are shooting for month-day values; your Question is convoluted so I am not sure of your goal. But if this is your goal, again, there’s a class for that: MonthDay
.
Month
Or maybe you want just the month regardless of year or day-of-month, in which case you can use the Month
class.
LocalDate
If your inputs strings represent a year and month and day-of-month, parse as a LocalDate
. This class has no time-of-day and no time zone.
LocalDate ld = LocalDate.parse( "2016-01-15" ) ;
Extract a YearMonth
, MonthDay
, or Month
.
YearMonth ym = YearMonth.from( ld ) ;
Create a collection. Perhaps you want to keep all a distinct set of the LocalDate
objects in a particular year-month. If so, make a Map
where each YearMonth
object owns a Set
of LocalDate
objects.
Map < YearMonth, Set < LocalDate > > map = new HashMap <>();
As you process each input date, check to see if the map has a Set
yet created for the particular YearMonth
of the input. If not, instantiate a TreeSet
. The TreeSet
class is a SortedSet
, meaning it maintains a sorted order as you add values.
Set < LocalDate > set = map.get( ym );
if ( null == set ) {
set = new TreeSet <>(); // A `TreeSet` is a `SortedSet`, maintains a sorted order. You may or may not need this behavior.
map.put( ym , set );
}
With a Set
in hand, add your LocalDate
.
set.add( ld );
After processing, you can get a collection of the YearMonth
keys from your Map
. And for each of those, you can retrieve the Set
it owns, and get a count of the elements contained.
Lamba & Streams
For shorter code, you might be able to use Lambda syntax & Streams with Map::computeIfAbsent
. I've seen this kind of code but have not yet tried it.
map.computeIfAbsent( key , k -> new TreeSet< LocalDate >() ).add( ld ) ;
Count only
If you want only the count, and don't care about the LocalDate
values, replace Set
as the “value” or you Map
with a Integer
object. Instead of retrieving the Set
and adding to it, retrieve the Integer
and increment it by adding one. Personally, in this kind of situation I find it best to collect the LocalDate
values to be examined for debugging/testing and/or for further use in other business logic.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.