Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:


November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:


1. Introduction

In this article, we'll study several options that we can use in Java to get the week number for a given date. We'll start by looking at a few options for legacy code using classes prior to Java 8. Afterward, we'll take a look at the newer Date Time API in the java.time package that was introduced in Java 8.

2. Before Java 8

Prior to Java 8, date and time calculations were performed mainly using the Date and Calendar classes. Typically we create a Calendar, and then it is possible to extract the information we need from it by using different constants.

2.1. Get Week Number Using Calendar Fields

Let's look at our first example:

Calendar calendar = Calendar.getInstance(locale); 
calendar.set(year, month, day); 
int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);

We simply create a Calendar instance for the given Locale and set the year, month, and day, and finally, we get the WEEK_OF_YEAR field from the calendar object. This will return the week number within the current year.

Now, let's take a look at how to invoke this method from one of our unit tests:

public void givenDateUsingFieldsAndLocaleItaly_whenGetWeekNumber_thenWeekIsReturnedCorrectly() {
    Calendar calendar = Calendar.getInstance(Locale.ITALY);
    calendar.set(2020, 10, 22);

    assertEquals(47, calendar.get(Calendar.WEEK_OF_YEAR));

We need to be careful when adopting this approach, as the month field in the Calendar class is zero-based. This means if we want to specify December, then we need to use the number 11, which can often lead to confusion.

2.2. Get Week Number Using Locale Setups

In this penultimate example, we'll take a look at what the effect of applying some additional settings to our Calendar can have:

Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);

int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);

The Calendar class defines two methods:

  • setFirstDayOfWeek
  • setMinimalDaysInFirstWeek

These methods have an impact on how we calculate the week number. Normally, both these values are taken from the Locale when creating the Calendar. But it is also possible to set the first day in the week and the minimal days in the first week of the year manually.

2.3. Locale Differences

Locale plays an important role in how the week number is calculated:

public void givenDateUsingFieldsAndLocaleCanada_whenGetWeekNumber_thenWeekIsReturnedCorrectly() {
    Calendar calendar = Calendar.getInstance(Locale.CANADA);
    calendar.set(2020, 10, 22);

    assertEquals(48, calendar.get(Calendar.WEEK_OF_YEAR));

In this unit test, we've only changed the locale of the Calendar to use Locale.CANADA instead of Locale.ITALY and now the week number returned is 48 instead of 47.

Both results are correct. As mentioned previously, this happens because each Locale has different settings for the setFirstDayOfWeek and setMinimalDaysInFirstWeek methods.

3. Java 8 Date Time API

Java 8 introduced new APIs for Date and Time to address the shortcomings of the older java.util.Date and java.util.Calendar.

In this section, we'll take a look at some options for getting the week number from the date using this newer API.

3.1. Get Week Number Using the Numerical Values

Likewise, as we saw before with Calendar, we can also pass year, month, and day values directly into LocalDate:

LocalDate date = LocalDate.of(year, month, day);
int weekOfYear = date.get(WeekFields.of(locale).weekOfYear());

The benefit over our pre-Java 8 examples is that we don't have the problem of the month field being zero-based.

3.2. Get Week Number Using Chronofield

In this final example we'll see how to use the ChronoField enumeration, which implements the TemporalField interface:

LocalDate date = LocalDate.of(year, month, day);
int weekOfYear = date.get(ChronoField.ALIGNED_WEEK_OF_YEAR);

This example is similar to using the Calendar.WEEK_OF_YEAR int constant that we saw previously, but using ChronoField.ALIGNED_WEEK_OF_YEAR.

4. Conclusion

In this quick tutorial, we illustrated several ways of getting the week number from a date using plain Java.

As always, the full source code of the article is available over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:


Generic footer banner
Comments are closed on this article!