How to get the week number from a date in Java
Especially in English-speaking countries, the use of the calendar week is very common. The calculation and conversion are done with the class LocalDate in Java 8 and with Calendar in Java before version 8.
For many applications, it is necessary to determine the week number from a date in Java. There are 2 cases to consider. Firstly, you can get the current week and secondly, you can get the week on a specific date. In the following examples, several possibilities are shown how you can get this information.
Before we start, it turns out that in Java there is a method that already returns the week number:
int numberWeekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
But this number of week is based on the region, so we could get values that we do not expect.
According to ISO_8601 standard:
[st_adsense]The first week of a year (week W01) is considered the one that contains the first Thursday of that year, or, the one that contains the 4th of January. The days of the week are represented numerically with one digit, the first day being Monday (day 1) and the last day being Sunday (day 7). The week therefore always starts on Monday.
So to get the number of week according to this ISO standard use the following code:
Calendar calendar = Calendar.getInstance(); calendar.setFirstDayOfWeek(Calendar.MONDAY); calendar.setMinimalDaysInFirstWeek(4); calendar.setTime(date); int numberWeekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
And with this code, we already get the week number correctly. We can also get the number of week with Java 8 using a LocalDate object as shown in the following example:
import java.time.LocalDate; import java.time.temporal.IsoFields; import java.time.Month; public class WeekNumber { public static void main(String []args) { LocalDate localDate = LocalDate.of(2021, Month.JANUARY, 3); int n = localDate.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR); System.out.println(n); } }
Output:
53
The line localDate.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR)
returns the calendar week for the date.
But be careful: This only applies to our calendar system in English-speaking countries according to ISO-8601. If you want to use a different system, the locale must be specified explicitly:
import java.time.*; import java.util.Locale; import java.time.temporal.*; public class WeekNumber { public static void main(String []args) { LocalDate date = LocalDate.of(2021, Month.JANUARY, 3); TemporalField weekofyear = WeekFields.of(Locale.CANADA).weekOfWeekBasedYear(); System.out.println(date.get(weekofyear)); } }
Output:
2
In Canada the week starts with a Sunday and the first week is the one in which the first of January falls. Therefore, Sunday, January 3rd, 2021 is the first day in the 2nd calendar week.
[st_adsense]
Complete example 1: Get the current week number from a date in Java
In Java, there are several ways to determine the current week number. One of them is shown below.
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class WeekNumber { public static void main(String []args){ String inputDate = "20201125"; String inputFormat = "yyyyMMdd"; SimpleDateFormat dateFormat = new SimpleDateFormat(inputFormat); Date date = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int weekNumber = calendar.get(Calendar.WEEK_OF_YEAR); System.out.println(weekNumber); } }
Output:
45
Complete example 2: Get the week number on a specific date in Java
But in many cases you don’t want the current week number in Java but the week at a certain date. For this problem there are also several possibilities. One of them is shown below.
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class WeekNumber { public static void main(String []args){ String inputDate = "20201125"; String inputFormat = "yyyyMMdd"; SimpleDateFormat dateFormat = new SimpleDateFormat(inputFormat); Date date = null; try { date = dateFormat.parse(inputDate); } catch (ParseException e) { e.printStackTrace(); } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int weekNumber = calendar.get(Calendar.WEEK_OF_YEAR); System.out.println(weekNumber); } }
Output:
48[st_adsense]