java

How to determine the first day of the week 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.

The date for a specific calendar week is calculated by creating any date in the year and then setting the calendar week. To get the first day of the week, set DayOfWeek.MONDAY.
 

Example :
import java.time.*;
import java.util.Locale;
import java.time.temporal.*;

public class WeekNumber {

 public static void main(String []args)
 {
    int year = 2020;
    int weekN = 22;
    LocalDate ldate = LocalDate.of(year, Month.JANUARY, 3);
    LocalDate day = ldate.with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, weekN);
    LocalDate start = day.with(DayOfWeek.MONDAY);
    System.out.println(start);
 }
}

Output:

2020-05-25

As shown above, you can of course use another locale.
 

java-mcq-multiple-choice-questions-and-answersJava MCQ – Multiple Choice Questions and Answers – OOPsThis collection of Java Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Java OOPs”.   1. Which of the…Read More mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *