Java Program to get day of week as string

Java 8Object Oriented ProgrammingProgramming

To get day of week as string, first let us display the current date −

LocalDate currentDate = LocalDate.now();

Now, use DayOfWeek to get the day of week from the current date −

DayOfWeek day = currentDate.getDayOfWeek();

Get the day of week as string −

String weekName = day.name();

Example

import java.time.DayOfWeek;
import java.time.LocalDate;
public class Demo {
   public static void main(String[] args) {
      LocalDate currentDate = LocalDate.now();
      System.out.println("Current Date = "+currentDate);
      DayOfWeek day = currentDate.getDayOfWeek();
      int weekVal = day.getValue();
      String weekName = day.name();
      System.out.println("Week Number = "+weekVal);
      System.out.println("Week Name = "+weekName);
   }
}

Output

Current Date = 2019-04-12
Week Number = 5
Week Name = FRIDAY
raja
Published on 26-Apr-2019 10:01:29
Advertisements