To achieve what you want without creating a LocalDateTime
instance, you can use the org.joda.time.chrono.ISOChronology
class to get a org.joda.time.DateTimeField
that corresponds to the day of week.
In the example below I'm using org.joda.time.DateTimeConstants
, which has values from Monday (1
) to Sunday (7
). I'm also using java.util.Locale
to get the names in English, but you can use whatever Locale
you want (the code in your question uses the system's default locale, in this case you could just use Locale.getDefault()
instead):
DateTimeField dayOfWeek = ISOChronology.getInstance().dayOfWeek();
System.out.println(dayOfWeek.getAsText(DateTimeConstants.MONDAY, Locale.ENGLISH));
System.out.println(dayOfWeek.getAsText(DateTimeConstants.TUESDAY, Locale.ENGLISH));
System.out.println(dayOfWeek.getAsText(DateTimeConstants.WEDNESDAY, Locale.ENGLISH));
System.out.println(dayOfWeek.getAsText(DateTimeConstants.THURSDAY, Locale.ENGLISH));
System.out.println(dayOfWeek.getAsText(DateTimeConstants.FRIDAY, Locale.ENGLISH));
System.out.println(dayOfWeek.getAsText(DateTimeConstants.SATURDAY, Locale.ENGLISH));
System.out.println(dayOfWeek.getAsText(DateTimeConstants.SUNDAY, Locale.ENGLISH));
The output is:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
New Java Date/Time API
Joda-Time is in maintainance mode and is being replaced by the new APIs, so I don't recommend start a new project with it. Even in joda's website it says: "Note that Joda-Time is considered to be a largely “finished” project. No major enhancements are planned. If using Java SE 8, please migrate to java.time (JSR-310).".
If you're using Java 8, consider using the new java.time API. It's easier, less bugged and less error-prone than the old APIs.
If you're using Java <= 7, you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes. And for Android, there's the ThreeTenABP (more on how to use it here).
The code below works for both.
The only difference is the package names (in Java 8 is java.time
and in ThreeTen Backport (or Android's ThreeTenABP) is org.threeten.bp
), but the classes and methods names are the same.
for(DayOfWeek dow : DayOfWeek.values()) {
System.out.println(dow.getDisplayName(TextStyle.FULL, Locale.ENGLISH));
}
This will also output:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
You can also get a DayOfWeek
from an int
value - from 1 (Monday) to 7 (Sunday) - using DayOfWeek.of
method:
// 1 is Monday, 2 is Tuesday and so on, until 7 (Sunday)
System.out.println(DayOfWeek.of(1).getDisplayName(TextStyle.FULL, Locale.ENGLISH));