For non-English day-of-week names, see Answer by tete.
tl;dr
DayOfWeek.valueOf( "Monday".toUppercase() ) // `DayOfWeek` object. Works only for English language.
.getValue() // 1
java.time
If your day-of-week names happen to be the full-length name in English (Monday, Tuesday, etc.), that happens to coincide with the names of the enum objects defined in the DayOfWeek
enum.
Convert your inputs to all uppercase, and parse to get a constant object for that day-of-week.
String input = "Monday" ;
String inputUppercase = input.toUppercase() ; // MONDAY
DayOfWeek dow = DayOfWeek.valueOf( inputUppercase ); // Object, neither a string nor a number.
Now that we have a full-feature object rather than a string, ask for the integer number of that day-of-week where Monday is 1 and Sunday is 7 (standard ISO 8601 definition).
int dayOfWeekNumber = dow.getValue() ;
Use DayOfWeek
objects rather than strings
I urge you to minimize the use of either the name or number of day-of-week. Instead, use DayOfWeek
objects whenever possible.
By the way, you can localize the day-of-week name automatically.
String output = DayOfWeek.MONDAY.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH );
That localization is one-way only through the DayOfWeek
class. To go the other direction in languages other than English, see the Answer by tete.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
- Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.