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);
}
} |
Week of the year: 7
Explanation of the above Program:
- First, we import the required packages: java.io for basic input-output operations and java.util.Calendar for working with dates and times.
- Now we define a class named WeekOfYear .
- Now we create an object named ‘instance’ of Calendar by using Calendar.getInstance().
- This object represents Current date and time.
- instance.get(Calendar.WEEK_OF_YEAR) returns an integer that represents the current week of the year .
- At last, it prints the week of the year.
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);
}
} |
Week of the year: 7
Explanation of the above Program:
- First, we import java.time.LocalDate , it is used to represent a specific date without its time-zone and java.time.temporal.WeekFields to access the week fields.
- Now we define a class named WeekOfYear.
- Inside main method we define an object named instance by using LocalDate.now(). This object represents the current date.
- instance.get(WeekFields.ISO.weekOfWeekBasedYear()) returns an integer which represents current date of the year based on ISO-8601 standard.
- After that we simply print it.
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);
}
} |
Week of the year: 5
Explanation of the above Program:
- First, we import java.time.LocalDate , it is used to represent a specific date without its time-zone and java.time.temporal.WeekFields to access the week fields.
- Now we define a class named WeekOfYear.
- Inside the main method, we create a LocalDate object named ‘date’ representing a particular specified date LocalDate.of().
- instance.get(WeekFields.ISO.weekOfWeekBasedYear()) returns an integer which represents current date of the year based on ISO-8601 standard.
- After that we simply print the week.