Open In App

How to Find Which Week of the Year in Java?

In Java, we have the Calendar class and the newer LocalDate class from the java.time package which we can use to find which week of the year it is. The java.time package offers better functionality and ease of use than older date and time classes such as Calendar class.

In this article, we will learn how to find which week of the year in Java.



Program to Find Which Week of the Year in Java

Below are the approaches and their code implementations to find the week of the year in Java.

Approach 1: Using Calendar class (Pre-Java 8)

In Java programming, two distinctive classes, named the Calendar class and LocalDate class found in java.time package. These classes serve the purpose of determining the specific week within a given year.






// Java program to find which week of the year 
import java.io.*;
import java.util.Calendar;
public class WeekOfYear {
    public static void main(String[] args) 
    {
        
      //get the current date
      Calendar instance=Calendar.getInstance();
        
      //get the week of the year
        int week = instance.get(Calendar.WEEK_OF_YEAR);
        
        System.out.println("Week of the year: "+ week);
    }
}

Output
Week of the year: 7

Explanation of the above Program:

Approach 2: Using Local Date class (Java 8 and later)

Local Date is a newer class introduced in Java 8 which we can use to calculate week number. WeekFields.ISO represents the ISO-8601 standard for weeks. We can create LocalDate instance using the now( ) method and of( ) method.

Using now( ) method:

It is used to get the current date and time based on the system clock and the time zone used is the system default time zone until explicitly specified.




// Java program to get week of the year
  
import java.time.LocalDate;
import java.time.temporal.WeekFields;
  
public class WeekOfYear 
{
    public static void main(String[] args) 
    {
        
        //get the current date
        LocalDate instance=LocalDate.now();
        
        //get week of the year
        int week= instance.get(WeekFields.ISO.weekOfWeekBasedYear());
        
        System.out.println("Week of the year: " + week);
    }
}

Output
Week of the year: 7

Explanation of the above Program:

Approach 3: Using of( ) Method

We can create a LocalDate instance using the ‘of method’ but We need to specify the year, month, and day.




import java.time.LocalDate;
  
public class WeekOfYear {
    public static void main(String[] args)
    {
        // Year, Month, Day
        LocalDate date = LocalDate.of(2024, 2, 2);
    }
}

After specifying the date, now, we can find the week of the year.




import java.time.LocalDate;
import java.time.temporal.WeekFields;
  
public class WeekOfYear {
    public static void main(String[] args){
        //set date
        LocalDate date = LocalDate.of(2024, 2, 2);
        //get number of week
        int week = date.get(WeekFields.ISO.weekOfWeekBasedYear());
          
        System.out.println("Week of the year: " + week);
    }
}

Output
Week of the year: 5

Explanation of the above Program:


Article Tags :