Get Day of Week from Date Object in Java

Java Open Source XChart Library

XChart features a dead-simple API for turning your data into beautiful charts including line, scatter, area, pie, donut, bubble, stick, bar, and histogram charts. Extra features include: real-time, themes, CSV import and export, export as PNG, JPG, BMP, GIF, SVG, EPS and PDF and much much more. For full description and examples visit the project on Github!

To get the day of the week from a java.util.Date object, we can use the java.text.SimpleDateFormat class. First, create a Date object. Next, create a SimpleDateFormat instance using the getDateInstance() method, passing the String “E” or “EEEE” as the argument. Get the day of the week as a String using the format() method passing in the java.util.Date object. To get the weekday in numerical format, we can use the java.util.Calendar object’s get method passing in Calendar.DAY_OF_WEEK.

Get Day of Week from Date in Java – Example Code

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

//

// The following example code demonstrates how to

// print out the Day of the Week from a Date object.

//

publicclassGetDayFromDate{

publicstaticvoidmain(String[]args){

Date now=newDate();

SimpleDateFormat simpleDateformat=newSimpleDateFormat("E");// the day of the week abbreviated

System.out.println(simpleDateformat.format(now));

simpleDateformat=newSimpleDateFormat("EEEE");// the day of the week spelled out completely

System.out.println(simpleDateformat.format(now));

Calendar calendar=Calendar.getInstance();

calendar.setTime(now);

System.out.println(calendar.get(Calendar.DAY_OF_WEEK));// the day of the week in numerical format

}

}

Here is the output of the example code for getting the day of the week: