×

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

Related Topics

How to Calculate the Time Difference between Two Dates in Java?

The date is used extensively in Java to calculate date discrepancies. The date of joining an organization, admittance, appointment, etc., can be included when creating the application. The differences between...

4 minutes read.

Catalan number in Java

In general mathematics, Catalan numbers can be defined as the sequence of natural numbers that frequently occur in counting problems often encountered in recursively defined objects. Mathematical formula of Catalan number Coming...

3 minutes read.

Skyline Problem in Java

The skyline of a city is the outer edge of the pattern created by all of its structures when viewed from a distance. Return the skyline that these buildings together...

4 minutes read.

How to check if date is valid in Java?

In this article, you will acknowledge about how to verify if a date is valid or not. For this you will learn the approach, you will be able to write...

3 minutes read.

How to Print array in Java?

A Java array is a data structure that allows us to hold components of the same data type. An array's items are kept in a single memory region. As a...

6 minutes read.

Swapping Program in Java

Swapping Program in Java The swapping program in Java is used to interchange the values of the two variables. For example, if X = 12 and Y = 24, then the...

4 minutes read.

Java Output Formatting

Sometimes we want a program's output to be displayed in a specific format. The printf () function in the C programming language can be used to achieve this. We will...

4 minutes read.

Types of Bitwise Operators in Java

In this tutorial, we will learn about the various types or kinds of bitwise operators in java. Before proceeding to bitwise operators, let us know what is meant by the...

6 minutes read.

Swastika Pattern in Java

This part teaches us how to create the Swastika Pattern in Java utilizing user-defined columns and rows as well as stars or other special characters.A Java pattern application that is...

2 minutes read.

Java class class

Java Class class instances are an executing Java application's implementation of the classes and interfaces. As well as, every Array is indeed an object that is common for all Arrays...

6 minutes read.

AES 256 Encryption in Java

Now a days, security has grown in importance. Java programming supports a variety of encryption and hashing methods, which offers security for data transport and communication among various nodes. In...

4 minutes read.

Java String toUpperCase() methods

Java String toUpperCase() method is used to convert all the characters of the String into upper case. Syntax public String toUpperCase() public String toUpperCase(Locale locale) Returns It returns upper case String. Java String toUpperCase() Example 1: public...

1 minute read.

How to add Elements in Array in Java

Consider an array of the size n, in the given size we must add the elements in the given array. In this tutorial we are preferred to learn how to add...

3 minutes read.

Copy data/content from one file to another in java

In this article, you will be acknowledged about how to copy data or content from one file to another file. Also, you will be acknowledged about the classes and methods...

3 minutes read.

Java String copyValueOf() method

copyValueOf() method returns a String that holds the character sequence of the character array. Syntax: copyValueOf(char[] data) Parameters: data : the character array i.e. String Returns: It returns a String that contains the characters of the...

2 minutes read.

Gregorian Calendar Java Current Date

GregorianCalendar class uses the Gregorian and Julian calendars. Dates are calculated by projecting present laws forever backward and forward in time. As a consequence, GregorianCalendar may be utilised to create...

8 minutes read.

Java Boolean compare() method

The compare() method of Java Boolean class compares the specified Boolean values and returns a positive 1 or negative 1 or zero integer value based on the result. Syntax public static int...

2 minutes read.

Compile-time Error in Java

In java, the execution of a program is stopped due to the occurrence of some problem known as an error. Errors are illegal operations that are carried out by the...

4 minutes read.

Java file Writer

File Writer: It is used to write all the data from text files. It inherits the properties from the Output Stream class. It is meant for writing characters. It is meant...

4 minutes read.

Difference between = = and equals ( ) in java

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

6 minutes read.