To work with the GregorianCalendar class, import the following package −
import java.util.GregorianCalendar;
To get the day of week in month, use the following field −
cal.get(Calendar.DAY_OF_WEEK_IN_MONTH)
Above, cal is the GregorianCalendar object we created before −
GregorianCalendar cal = new GregorianCalendar();
The following is an example −
import java.util.GregorianCalendar; import java.util.Calendar; import java.util.Date; public class Demo { public static void main(String[] args) { GregorianCalendar cal = new GregorianCalendar(); // date information System.out.println("Date Information.........."); System.out.println("Year = " + cal.get(Calendar.YEAR)); System.out.println("Month = " + (cal.get(Calendar.MONTH) + 1)); System.out.println("Date = " + cal.get(Calendar.DATE)); // week System.out.println("Day of Week in month = " + cal.get(Calendar.DAY_OF_WEEK_IN_MONTH)); } }
Date Information.......... Year = 2018 Month = 11 Date = 19 Day of Week in month = 3