Java Program to Write Display Weekday

In this article we are going to see how to display weekday in Java along with suitable examples.

Java Program to Write Display Weekday

The java.text.SimpleDateFormat class provides inbuild methods to format the date and time in java.

There are 2 patterns which we can use in SimpleDateFormat to display the weekday.

  1. EEE: used for displaying weekday in short form.
  2. EEEE: used for displaying weekday in full form.

Let’s see the program to understand it clearly.

Method-1: Java Program to Write Display Weekday By Using EEEE Format

Approach:

  • Create another object of SimpleDateFormat as ‘s’ with the argument as ‘EEEE’.
  • Declare a string variable as ‘day’ and initialize it to the current date and time using an inbuild method of SimpleDateFormat as s.format(new Date())
  • Print the result.

Program:

import java.text.SimpleDateFormat;
import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
    //create an object of SimpleDateFormat as ‘s’ with the argument as ‘EEEE’.
    SimpleDateFormat s = new SimpleDateFormat("EEEE");
    // Declare a string variable as ‘month’ 
    //and initialize it to the current date and time 
    //using an inbuild method of SimpleDateFormat as s.format(new Date())
    String day= s.format(new Date());
    //Print the result in fullform
    System.out.println("Weekday in fullform is "+day);
    }
}

Output:

Weekday in full form is Friday

Method-2: Java Program to Write Display Weekday By Using EEE Format

Approach:

  • Create another object of SimpleDateFormat as ‘s’ with the argument as ‘EEE’.
  • Declare a string variable as ‘day’ and initialize it to the current date and time using an inbuild method of SimpleDateFormat as s.format(new Date())
  • Print the result.

Program:

import java.text.SimpleDateFormat;
import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
    //create an object of SimpleDateFormat as ‘s’ with the argument as ‘EEE’.
    SimpleDateFormat s = new SimpleDateFormat("EEE");
    // Declare a string variable as ‘month’ 
    //and initialize it to the current date and time 
    //using an inbuild method of SimpleDateFormat as s.format(new Date())
    String day= s.format(new Date());
    //Print the result in fullform
    System.out.println("weekday in fullform is "+day);
    }
}

Output:

Weekday in full form is Fri

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.