Get week of month and year using Java Calendar



For using Calendar class, import the following package.

import java.util.Calendar;

Create a Calendar class object.

Calendar cal = Calendar.getInstance();

Now, get the week of month and year using the following fields.

Calendar.WEEK_OF_MONTH Calendar.WEEK_OF_YEAR

The following is an example.

Example

 Live Demo

import java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar cal = Calendar.getInstance();       // current date and time       System.out.println(cal.getTime().toString());       // date information       System.out.println("\nDate 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("Week of month = " + cal.get(Calendar.WEEK_OF_MONTH));       System.out.println("Week of year = " + cal.get(Calendar.WEEK_OF_YEAR));       // time information       System.out.println("\nTime Information..........");       System.out.println("Hour (24 hour format) : " + cal.get(Calendar.HOUR_OF_DAY));       System.out.println("Hour (12 hour format) : " + cal.get(Calendar.HOUR));       System.out.println("Minute : " + cal.get(Calendar.MINUTE));       System.out.println("Second : " + cal.get(Calendar.SECOND));       System.out.println("Millisecond : " + cal.get(Calendar.MILLISECOND));    } }

Output

Mon Nov 19 14:28:46 UTC 2018 Date Information.......... Year = 2018 Month = 11 Date = 19 Week of month = 4 Week of year = 47 Time Information.......... Hour (24 hour format) : 14 Hour (12 hour format) : 2 Minute : 28 Second : 46 Millisecond : 464
Updated on: 2020-06-27T09:01:18+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements