3

I have a primitive int variable with possible values from 1 to 7 (actually it received from calling getDayOfWeek() on org.joda.time.DateTime object and then persisted).

Now I need to get a string representation of this day of week. I can do it by creating a new DateTime object and setting that day of week to created object:

LocalDateTime.now().withDayOfWeek(dayOfWeek).dayOfWeek().getAsText();

The question is: can I get the same string without redundant creation of a LocalDateTime object?

Peter
  • 1,512
  • 1
  • 22
  • 40
  • Yes. The values start with `Monday` as `1`, and end with `Sunday` as `7`. See also [`DateTimeConstants`](http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTimeConstants.html). – Elliott Frisch Jul 12 '17 at 03:53
  • Sorry, there is no better way. By the way, I have found this way in my [answer](https://stackoverflow.com/questions/20907809/from-the-day-week-number-get-the-day-name-with-joda-time/20907968#20907968) dated from 2014. The best workaround if you only need English might be just holding a static String-array ("Monday" to "Sunday") and find the string by array index. – Meno Hochschild Jul 12 '17 at 07:17
  • @MenoHochschild I also thought that it was impossible, but I've actually found a way (check my answer below). Anyway, maybe a string array is simpler, if you don't care about a locale sensitive solution. –  Jul 12 '17 at 16:28

2 Answers2

4

The Joda-Time project is now in maintenance mode, so it is advised to migrate to the java.time classes. There is a simple and easy solution with java.time for your scenario using DayOfWeek enum as shown in the example below:

int dayOfWeek = 1;
System.out.println(DayOfWeek.of(dayOfWeek)); // prints English all-caps, ex: MONDAY

Call DayOfWeek::getDisplayName to generate a string of the day’s name localized in a human language you specify via a Locale object.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Pallavi Sonal
  • 3,661
  • 1
  • 15
  • 19
4

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));
  • 1
    Hugo, thank you! I didn't know about this backport and this project is exactly on Android. I'll try to switch to ThreeTen until it's not too late. – Peter Jul 13 '17 at 02:07
  • I've migrated the project to ThreeTen and updated all unit tests. All is well. – Peter Jul 21 '17 at 01:30
  • @dpg That's great, it's totally worth it! And an eventual migration to java 8 will be much easier as it would require just a package renaming in the imports. –  Jul 21 '17 at 01:32