How Do You Get The First Day Of The Week In Java

Chapter: Date and Time Last Updated: 15-09-2023 06:26:25 UTC

Program:

            /* ............... START ............... */
                

// Using java.util.Calendar (prior to Java 8):

import java.util.Calendar;

public class FirstDayOfWeekExample {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
        
        System.out.println("First day of the week: " + calendar.getTime());
    }
}

// Using java.time (Java 8 and later):

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;

public class FirstDayOfWeekExample {
    public static void main(String[] args) {
        LocalDate currentDate = LocalDate.now();
        LocalDate firstDayOfWeek = currentDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        
        System.out.println("First day of the week: " + firstDayOfWeek);
    }
}

                /* ............... END ............... */
        

Output

Sample Output Using java.util.Calendar (prior to Java 8):
First day of the week: Mon Sep 12 00:00:00 UTC 2023

Sample Output Using java.time (Java 8 and later):
First day of the week: 2023-09-11




Notes:

  • In first code uses Calendar.getInstance() to get the current date and time, and then sets the DAY_OF_WEEK field to the value of getFirstDayOfWeek(), which represents the first day of the week for the default locale.
  • In Java 8 and later, the java.time package provides a more modern and easier-to-use API for working with dates and times. In this example, we use LocalDate.now() to get the current date, and then use TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY) to find the first day of the week (assuming Monday as the first day).
  • Choose the method that best fits your Java version and coding style.
Similar Programs Chapter Last Updated
Java Program to Calculate Time Difference In Hours Date and Time 20-09-2023
Java Code To Calculate Time Difference In Milliseconds Date and Time 20-09-2023
Java Program To Calculate Time Difference In Seconds Date and Time 20-09-2023
How to calculate time difference between two time zones in Java Date and Time 20-09-2023
Java Date And Time API To Create A Custom Calendar Date and Time 20-09-2023
How To Format Date And Time In Java Date and Time 16-09-2023
Java Code To Change Date Format Date and Time 15-08-2023
Java Program To Find Day Of The Week For A Given Date Date and Time 10-08-2023
Java code to check if date is weekend Date and Time 29-06-2023
Java program to find number of days between two dates Date and Time 29-06-2023
Java Program To Get The Current Time In Different Time Zones Date and Time 22-06-2023
How To Add Working Days To A Date In Java Date and Time 22-06-2023
Java Program To Add A Specified Number Of Days To A Given Date Date and Time 22-06-2023
Write A Program To Check Leap Year In Java Date and Time 22-06-2023
Write A Program To Display Current Date And Time In Java Date and Time 22-06-2023
Java Date Format AM PM Date and Time 14-05-2023
Java Add Months To Date Date and Time 14-05-2023
Add Days To Date In Java example Date and Time 14-05-2023
Java Program To Calculate Days Between Two Dates Date and Time 16-04-2023
Java Program To Add Days To Date Date and Time 16-04-2023
How To Find Difference Between Two Dates In Java Date and Time 01-04-2023
Add Days To Date Java Example Date and Time 08-08-2021
Time Difference Between Two Timestamps In Java Date and Time 22-09-2018

1