I need to calculate second monday of current year in one of program. a method in java which tells what is the date on second monday of current year using Gregorian Calendar. Please suggest.
-
What have you tried so far? You could try using the `Calendar` class and setting the month to January (since it's definitely the 2nd monday of the year will be in Jan), and getting the Date of the first day of the week. – Zhi Kai Oct 13 '16 at 03:39
3 Answers
You could have written something like this
int year = 2016;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, 2);
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.YEAR, year);
return cal.get(Calendar.DATE);

- 585
- 8
- 16
-
Shouldn't it be `WEEK_OF_MONTH` instead of `DAY_OF_WEEK_IN_MONTH` being set to 2 to set it to the 2nd week's Monday in January 2016? – Zhi Kai Oct 13 '16 at 03:45
-
1if the first week of the month does not start on a monday, using `WEEK_OF_MONTH` set to 2 will still give the first monday of the month. – Samuel Kok Oct 13 '16 at 03:52
-
Set calendar to the first day of the month jan.
Retrieve its day of the week
Calculate the date of the first Monday of the month.
Add 7 days using calendar.add() method. You will get the second Monday.

- 3,133
- 5
- 30
- 59
-
Not always correct. There are week numbering systems that can start week 1 prior to Jan 1st. – Jim Garrison Oct 13 '16 at 05:19
tl;dr
java.util.GregorianCalendar.from(
LocalDate.now( ZoneId.of( "America/Montreal" ) )
.with( TemporalAdjusters.firstDayOfYear() )
.with( TemporalAdjusters.dayOfWeekInMonth( 2 , DayOfWeek.MONDAY ) )
.atStartOfDay( ZoneId.of( "America/Montreal" ) )
)
Avoid legacy date-time classes
The GregorianCalendar
class is now legacy, supplanted by the java.time classes, specifically ZonedDateTime
. Avoid the old legacy date-time classes as they are poorly-designed, confusing, and troublesome.
LocalDate
For a date-only value, use LocalDate
. The LocalDate
class represents a date-only value without time-of-day and without time zone.
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
TemporalAdjuster
The TemporalAdjuster
interface provides for classes to manipulate date-time values. The TemporalAdjusters
(note plural 's') class provides several handy implementations.
We an adjuster to get the first day of the current year, and another to get the second Monday of that month.
LocalDate firstOfYear = today.with( TemporalAdjusters.firstDayOfYear() );
LocalDate secondMondayOfYear = firstOfYear.with( TemporalAdjuster.dayOfWeekInMonth( 2 , DayOfWeek.MONDAY ) );
GregorianCalendar
If you really need a GregorianCalendar
you can convert by calling new methods added to the old classes. I assume you want the time-of-day set to the first moment of the day. That first moment is not always 00:00:00
so let java.time determine that time-of-day.
ZonedDateTime zdt = secondMondayOfYear.atStartOfDay( z );
GregorianCalendar gc = java.util.GregorianCalendar.from( zdt );
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
, & java.text.SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
- Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
- See How to use….
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.

- 1
- 1

- 303,325
- 100
- 852
- 1,154