How to find day of week in java 8 for any given date

 In earlier version of java,you need to write lines of code to find day of week of a given date. In java 8, you can find it  in a single line of code. Java  8 has come up with a new date and time API (java.time) that  contains many classes such as  LocalDate, LocalTime, LocalDateTime etc. which are thread safe and  very user friendly to do many operations on date. 


In older version of Java, you can find day of week using  Calendar class or  DateFormat which is not thread safe.  Now let us see how to find Day of week using LocalDate class of Java 8  and also using old Calender class &  DateFormat.

package testpack;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class dateTest {

 public static void main(String arg[]) throws ParseException
   dateTest dt=new dateTest();
 //Using Java 8 - Accepts date as string
  private  DayOfWeek dowUsingJava8 (String dateStr)
  DayOfWeek dayofWeek=LocalDate.parse(dateStr,DateTimeFormatter.ofPattern("yyyy-MM-dd")).getDayOfWeek();
   return dayofWeek;

 //Using Java 8 - Accepts year, month, day as int
  private  DayOfWeek dowUsingJava8 (int year, int month, int day)
  DayOfWeek dayofWeek =;
   return dayofWeek;

 //Using SimpleDateFormat - Accepts date as string
  private String dowUsingDateFormat(String dateStr) throws ParseException
    SimpleDateFormat in=new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat out=new SimpleDateFormat("EEEE"); 
    Date date=in.parse(dateStr);
    String dow=out.format(date);
    return dow;
 //Using CalenderClass- Accepts year, month, day as int
 private  String dowUsingCalenderClass(int yyyy, int mm, int dd)  {
         Calendar GC = new GregorianCalendar();
        GC.set(yyyy , mm-1, dd ,12,0,0);
        int DOW = GC.get(Calendar.DAY_OF_WEEK);    // 6=Friday
        return weekday(DOW-1);

 // To map day of week numeric to day of week 
 private static String weekday(int DOW)
    String dow="";
    switch (DOW) {
             case 0:  dow = "Sunday";       break;
             case 1:  dow = "Monday";       break;
             case 2:  dow = "Tuesday";       break;
             case 3:  dow = "Wednesday";     break;
             case 4:  dow = "Thursday";       break;
             case 5:  dow = "Friday";       break;
             case 6:  dow = "Saturday";       break;
   return dow;

Output of all sop : WEDNESDAY

Leave a Reply