Java Calendar get Xº day of the week of month

93 views Asked by At

I'm need to do a method that returns the position of actual day in the next month.

for today (20/12/2016)
  I need to call this method whit today date
  The return given must to be (17/01/2016)
  This method must return the third Tuesday of the next month
  Is the 4 week of this month, but I need the Third Tuesday.

I try to use Calendar.MONTH, Calendar.WEEK_OF_MONTH, Calendar.DAY_OF_WEEK but I can't get the third, I always get the fourth.

Some thing like this:

public static Date getNextMonthDayOfWeel(Date d) {
    Calendar c = Calendar.getInstance();
    c.setTime(d);

    int week = c.get(Calendar.WEEK_OF_MONTH);
    int day = c.get(Calendar.DAY_OF_WEEK);

    //// this block
    if(!firstWeekOfMonthHad(day)){
        week++;
    }
    //// this block

    c.setFirstDayOfWeek(Calendar.SUNDAY);

    c.add(Calendar.MONTH, 1);
    c.set(Calendar.WEEK_OF_MONTH, week);
    c.set(Calendar.DAY_OF_WEEK, day);

    return c.getTime();
}

How can I get if the first week of the month have the specific day of the week?

2

There are 2 answers

3
Viktor Mellgren On

If you use Java 8 or later, it is as simple as:

LocalDate.now()
.plusMonths(1)
.with(TemporalAdjusters.firstInMonth(DayOfWeek.TUESDAY))
.plusWeeks(3);
0
Basil Bourque On

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.