Java program to find the first and last day of the week

To calculate the first and last day of each week, you should be aware of the text package in JAVA. “java.text” package is necessary for every developer. Because it has a lot of classes predefined. They are helpful in formatting such as dates, numbers, and also messages.

java.text classes

classes in java.text are as follows:

  • simple Date Format: It is a predefined class which helps in formatting and parsing dates.
  • Message Format: It is a message formatter. It is same as printf( ) in C language.
  • Choice Format: It maps numerical ranges to text. It is constructed by specifying the numerical ranges and also the strings that correspond to them.

As our program is on days and weeks, we’ll concentrate on simple Date Format.

simple Date Format:

        As we have seen above, simple Date Format is a predefined class which helps in formatting and parsing dates. For example, in your business requirements, you want to transform the date format from “year/month/date” to “month/date/year”.  simple Date Format helps in fulfilling this with the help of its attributes.

       One of the main uses of simple Date Format is, it has the ability to interpret a string date format to date object. That means we can manipulate the data as our wish. For example, if you want to subtract the day from the date which is in the form of string, there is no other way than to convert the string form to a date object. It is easily using the method of simple Date Format.

SimpleDateFormat Class Syntax:

public class SimpleDateFORMAT Extends DateFormat

Find the first day and last day of the week in Java

Following is the program to calculate the first and last of each week in java:

import java.util.*;
import java.text.*;

public class Main 
       public static void main(String []args)
           //sets the calendar to current date and time
           Calendar date = Calendar.getInstance();
           //sets the calendar with starting day of week
           date.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
           //printing of first and last day of th week
           DateFormat dateformat = new SimpleDateFormat("EEE dd/MM/yyyy");
           for (int i = 0; i<6; i++)
                 date.add(Calendar.DATE, 1);


Sun 30/06/2019                                                                                                                                     
Sat 06/07/2019


  • Initially, the packages required for the program are imported that are util and text packages.
  • getInstance() method is used to get the current date and time.
  • As our week starts from Sunday there are predefined values for weeks in java.
  1. Sunday–0
  2. Monday–1
  3. Tuesday–2
  4. Wednesday–3
  5. Thursday–4
  6. Friday–5
  7. Saturday–6
  • After getting the current date and time, current day of the week is set to Sunday.
  • In for loop, if the day is the last one i.e; Saturday the loop ends.
  • If it is not Saturday, the value of the day adds to the current value.

Get the name of first and last day of a month in Java


Leave a Reply

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