Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Date from Week Number, Year and dayOfWeek in java?

Tags:

java

date

I have the week number, its curresponding year and dayOfWeek number(i.e. 1 for Monday, 2 for Tuesday and so on). Is there a way to find the date with this information in java?

Following is a method I found online.

int week = 51;
LocalDate wkstart = LocalDate.now().with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, week);
LocalDate mon = wks.plusDays(1);
LocalDate tue = wks.plusDays(2);
LocalDate wed = wks.plusDays(3);
LocalDate thu = wks.plusDays(4);
LocalDate fri = wks.plusDays(5);
LocalDate sat = wks.plusDays(6);
LocalDate wkend = wks.plusDays(7);

But then realised that wkstart is storing the current date instead of the start of the week.
Is there a better way of doing this?

like image 974
Ebenezer Isaac Avatar asked Dec 18 '19 04:12

Ebenezer Isaac


People also ask

How to get Day of week in Java?

To get the day of the week, use Calendar. DAY_OF_WEEK.

How to get week of year Java?

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.


1 Answers

It also depends on the Locale.

Note that the first day of the week is Locale-dependent e.g. it is Monday in the UK while Sunday in the US. As per the ISO 8601 standards, it is Monday. For comparison, check the US calendar and the UK calendar. Accordingly, the date will vary as shown in the example below:

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.WeekFields;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        // Test
        int weekNumber = 34;
        int year = 2021;
        
        System.out.println(getLocalDate(weekNumber, DayOfWeek.TUESDAY, year, Locale.UK));
        System.out.println(getLocalDate(weekNumber, DayOfWeek.TUESDAY, year, Locale.US));
        
        System.out.println(getLocalDate(weekNumber, DayOfWeek.SUNDAY, year, Locale.UK));
        System.out.println(getLocalDate(weekNumber, DayOfWeek.SUNDAY, year, Locale.US));        
    }

    static LocalDate getLocalDate(int weekNumber, DayOfWeek dow, int year, Locale locale) {
        return LocalDate.of(year, 2, 1)
                .with(dow)
                .with(WeekFields.of(locale).weekOfWeekBasedYear(), weekNumber);
    }
}

Output:

2021-08-24
2021-08-17
2021-08-29
2021-08-15

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

like image 171
Arvind Kumar Avinash Avatar answered Dec 11 '22 09:12

Arvind Kumar Avinash