How to Calculate Week Number From Current Date in Java?
The WeekFields class's weekOfMonth() method is utilized to return the field for access the week of a month based on this WeekFields.
- If the first day of the month is a Monday, week one begins on the first and there is no week zero.
- If the the first day of the month is a Monday, week one begins on the first and there is no week zero.
- If the 4th of the month is a Monday, week one begins on the 4th, and weeks zero begin on the 1st.
- If the 5th of the month is a Monday, week two begins on the 5th, while week one begins on the 1st.
This field is suitable with any calendar system.
Syntax:
public TemporalField weekOfMonth()
Parameters: This technique will not take anything.
Return value: These method doesn't really return null, instead it returns a field with access to the week-based-year.
The WeekFields.weekOfMonth() method is demonstrated in the following applications:
Filenumber: WeekNumber1.java
// Java application to demonstrate
// WeekFields.weekOfMonth() method
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalField;
import java.time.temporal.WeekFields;
public class WeekNumber1{
public static void main(String[] args)
{
// build WeekFields
WeekFields weekFields= WeekFields.of(DayOfWeek.MONDAY, 1);
// apply weekOfMonth()
TemporalField weekOfMonth= weekFields.weekOfMonth();
// LocalDate class is being created
LocalDate day= LocalDate.of(2022, 12, 23);
// get week of month for localdate
int A = day.get(weekOfMonth);
// Result is being printed
System.out.println("Month for the Week "+ day + " :" + A);
}
}
Output:
Month for the Week 2022-12-23 :4
Filename: WeekNumber2.java
// Java application to demonstrate
// WeekFields.weekOfMonth() method
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalField;
import java.time.temporal.WeekFields;
public class WeekNumber2 {
public static void main(String[] args)
{
// Build WeekFields
WeekFields weekFields= WeekFields.of(DayOfWeek.SUNDAY, 1);
// apply weekOfMonth()
TemporalField weekOfMonth= weekFields.weekOfMonth();
// LocalDate is being created
LocalDate day = LocalDate.of(2022, 12, 24);
// obtain the week of the month for localDate
int wom = day.get(weekOfMonth);
// Result is beign printed
System.out.println("Month of the Week"+ day + " :" + wom);
}
}
Output:
Month of the Week 2022-12-24 :4
Week Number of the Year
Filename: WeekNumber3.java
import java.util.*;
public class WeekNumber3{
public static void main(String[] args) throws Exception {
Date d1 = new Date();
Calendar c1 = Calendar. getInstance();
c1.setTime(d1);
System.out.println("Today is the " + c1.WEEK_OF_YEAR+ " Week of the year ");
System.out.println("Today is the "+c1.DAY_OF_MONTH + " Month of the year");
System.out.println("Today is the "+c1.WEEK_OF_MONTH +" Month of the year");
}
}
Output:
Today is the 3 Week of the year
Today is the 5 Month of the year
Today is the 4 Month of the year
Filename: WeekNumber4.java
import java.util.Calendar;
public class WeekNumber4 {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
System.out.println("Present week in the month is: " +cal.get(Calendar.WEEK_OF_MONTH));
System.out.println("Present Week of the year is : " +cal.get(Calendar.WEEK_OF_YEAR));
cal.add(Calendar.WEEK_OF_MONTH, 1);
System.out.println("After one year : " + (cal.get(Calendar.MONTH) + 1)+ "-"+ cal.get(Calendar.DATE)+ "-"+ cal.get(Calendar.YEAR));
}
}
Output:
Present week in the month is: 5
Present Week of the year is : 53
After one year : 1-1-2023