-2

I'm new in ENUMS. I'm looking for possibility to translate English names of days to Polish using ENUM class. What actually would I like to do? In my program I'm using LocalDate library and getDayOfWeek() method. My goal is made something like this: String day = Days(someDate.getDayOfWeek().toString()) and if someDate.getDayOfWeek() is MONDAY, the method give me translated day "Poniedziałek".

public enum Days {
     MONDAY {
        public String toString() {
            return "Poniedziałek";
        }
    },
// other days ...
}

It is possible with enums? Or there is something better solution?

Abdelghani Roussi
  • 2,707
  • 2
  • 21
  • 39
Seldo97
  • 611
  • 1
  • 8
  • 17

1 Answers1

1

It is possible with enums?

Of course, but not necessary.

Or there is something better solution?

Yes, use the built in functionality. Java already supports representation of dates in human readable form. Use the below code:

DayOfWeek.MONDAY.getDisplayName(TextStyle.FULL, Locale.forLanguageTag("pl-PL"));

It returns:

poniedziałek

Magnilex
  • 11,584
  • 9
  • 62
  • 84
  • This is the best approach! But how about capitalized names? – Marcin Rzepecki Oct 31 '20 at 19:02
  • Nice solution. Thank you. – Seldo97 Oct 31 '20 at 19:03
  • @marcin.programuje I am not sure why the polish locale does not have a capitalized first letter. Using english, you'd get "Monday". You can use the solution here: https://stackoverflow.com/questions/3904579/how-to-capitalize-the-first-letter-of-a-string-in-java – Magnilex Oct 31 '20 at 19:43