tl;dr
LocalDate.now( ZoneId.of( "Africa/Tunis" ) ) // Get current date in this particular time zone.
.plusMonths( 1 ) // Move to equivalent date in the month after.
.with(
TemporalAdjusters.dayOfWeekInMonth( 3 , DayOfWeek.TUESDAY ) // Move to the ordinal number occurrence of a day-of-week within this month.
)
Details
You seem to be asking for the third Tuesday of next month.
The Answer by Mellgren is good, but here's a variation on that idea using a more appropriate TemporalAdjuster
.
Avoid legacy classes
You are using troublesome old date-time classes that are now legacy, supplanted by the java.time classes.
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.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
Move to the next month.
LocalDate monthLater = today.plusMonths( 1 );
TemporalAdjuster
To move to the third Tuesday of the month, use an implementation of TemporalAdjuster
found in the TemporalAdjusters
class.
For an ordinal day-of-week within a month like “Third Tuesday of the month” or “First Thursday of the month”, Java offers a specific adjuster: TemporalAdjusters.dayOfWeekInMonth
. Pass the ordinal number such as 3
for “third”, and a DayOfWeek
enum object constant such as DayOfWeek.TUESDAY
.
TemporalAdjuster ta = TemporalAdjusters.dayOfWeekInMonth( 3 , DayOfWeek.TUESDAY ); // Pass ordinal number and `DayOfWeek`.
LocalDate thirdTuesdayOfNextMonth = monthLater.with( ta );
Dump to console.
System.out.println( "today is " + today + " in zone " + z );
System.out.println( "Third Tuesday of next month is " + thirdTuesdayOfNextMonth );
See this code run live at IdeOne.com.
Today is 2018-01-20 in zone America/Montreal
Third Tuesday of next month is 2018-02-20
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.
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.