12

I am working on a program that asks the user which day they would like to see a lunch menu for. They can enter any day by name (Monday, Tuesday, etc.). This works well, but I would also like them to be able to enter "Today" and then have the program get the current date and then check the menu for that value.

How would I do this?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
the-alexgator
  • 298
  • 1
  • 5
  • 11
  • 3
    http://stackoverflow.com/questions/5270272/how-to-determine-day-of-week-by-passing-specific-date – Amir Nov 23 '12 at 00:21

4 Answers4

18

You can use java.util.Calendar:

Calendar calendar = Calendar.getInstance();
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
Sean Powell
  • 1,397
  • 1
  • 9
  • 17
12

This is the exact answer of the question,

Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
System.out.println(new SimpleDateFormat("EE", Locale.ENGLISH).format(date.getTime()));
System.out.println(new SimpleDateFormat("EEEE", Locale.ENGLISH).format(date.getTime()));

Result (for today):

Sat

Saturday

Miral Sarwar
  • 327
  • 6
  • 12
10

As of Java 8 and its new java.time package:

DayOfWeek dayOfWeek = DayOfWeek.from(LocalDate.now());

or

DayOfWeek dayOfWeek = LocalDate.now().getDayOfWeek();
Clément Mangin
  • 423
  • 3
  • 8
6

Java 8 :

LocalDate.now().getDayOfWeek().name()
Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85