×

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 take String Input in Java

There are various ways to take String input in Java. In this section, we are going to discuss how to take String input in Java. There are following ways to...

5 minutes read.

Static vs Non-Static in Java

A static method can access and update the value of a static data member. The static keyword is mostly used in Java for memory management. The static keyword can be...

6 minutes read.

Object Definition in Java

In this article, you will be acknowledged about object in Java, its definition and how is this useful in writing the programs in Java. The comprehension of object-oriented technology depends on...

4 minutes read.

Package naming convention in Java

It is conceivable that many programmers will use the same name for various types, given that Java programmers from all over the world create classes and interfaces. For illustration, suppose...

4 minutes read.

Segment Tree in Java

Binary trees can address a variety of issues; however, the Segment Tree is more efficient in terms of time complexity. The segment tree in Java is represented using an array. Native...

4 minutes read.

Deque in Java

Deque in java collections with Example Deque is short for “double-ended queue.” It is a linear collection that extends the Queue interface and supports insertion and deletion of the element at both the...

3 minutes read.

Java Pass-by-Reference

In Java, passing parameters can be done using one of two fundamental methods. Pass-by-value is used for the first and pass-by-reference is used for the second. One thing to keep...

2 minutes read.

Longest Odd Even Subsequence in Java

In order to solve the Java problem known as the longest odd-even subsequence, one must identify a sequences in a non-negative array having size s that alternately includes odd and...

6 minutes read.

Java Math tan() Method

The tan() method of Java Math class returns the trigonometric tangent of the specified angle. Syntax: public static double tan(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The tan() method...

2 minutes read.

How to Read XML Files in Java?

Introduction Today, we are going to learn about how to read XML files in java. Before, reading the XML Files let us learn about XML. XML Files The full form of XML is...

8 minutes read.

Java String toCharArray() method

Java String toCharArray() method converts current String into character array. Syntax: public char[] toCharArray() Returns: It returns a character array Java String toCharArray() method example 1 import java.util.Arrays; public class JavaStringToCharArrayEx1 {           public static...

2 minutes read.

Crown Pattern in Java

We know the importance of solving pattern problems. We can solve pattern problems by using any programming language. There is no rule to translating into a particular programming language. We...

4 minutes read.

Java Read File to String

There are different ways to deal with forming and examining a text record. This is normal while dealing with various applications. There are different ways to deal with looking at a...

6 minutes read.

How to enable java in chrome

The Java module is huge for the Java Runtime Environment (JRE). It permits a program to work with the Java stage to run Java applets. Essentially, each of the undertakings connect...

3 minutes read.

Compare Two Times in Java

Definition The compareTo() function of the LocalTime class is used to compare times. The class's compareTo() method compares two LocalTime objects. Here we have two objects that have to be compared: the...

4 minutes read.

Static Array in Java

In this tutorial, we will study static arrays in Java. An array is a data structure that is of great importance in any programming language. It is classified into two...

3 minutes read.

Difference between Aggregation and Composition in Java

What is Aggregation? Before we start discussing Aggregation, we have to know some general information about our classes in java. Generally we have two members in our classes , the first...

8 minutes read.

Stone Game in Java

In this tutorial, we will learn to design a stone game in Java. First of all, we will understand what is this game all about. We will grasp it through...

9 minutes read.

Intersection Point of two linked list in Java

In this article, you will be very well acknowledged about how to achieve the intersection point of two linked list in Java. There are several approaches to obtain. Surely each...

8 minutes read.

Java Maven Silicon

Maven is a build automation tool that is mostly used for Java applications. It allows you to manage the build, reporting, and documentation of a project from the central location. Maven provides...

4 minutes read.