the-alexgator
the-alexgator

Reputation: 308

Get Current Day (Monday, Tuesday, etc) in Java

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?

Upvotes: 12

Views: 43469

Answers (4)

Mehraj Malik
Mehraj Malik

Reputation: 15864

Java 8 :

LocalDate.now().getDayOfWeek().name()

Upvotes: 7

Clément Mangin
Clément Mangin

Reputation: 434

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

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

or

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

Upvotes: 10

Sean Powell
Sean Powell

Reputation: 1437

You can use java.util.Calendar:

Calendar calendar = Calendar.getInstance();
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

Upvotes: 18

Miral Sarwar
Miral Sarwar

Reputation: 327

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

Upvotes: 12

Related Questions