5

I need to get a day string with only a day of week integer (example : 1 = monday, 5 = friday...), I don't have any date.

I know that I can define an array of string but I think there is a better solution by using a date format. Is it possible without having a date ?

Alex R.
  • 861
  • 1
  • 13
  • 28
  • 1
    a enum sounds better – Scary Wombat Aug 17 '16 at 07:52
  • Dupe retracted: The other question is about Weekday from Date. Sorry 'bout that. – Fildor Aug 17 '16 at 08:50
  • @Fildor That is indeed a duplicate, showing how to get a localized name of day-of-week. – Basil Bourque Aug 17 '16 at 14:32
  • @BasilBourque The accepted answer on this post is not correct in my case because I just have a day of week [0-6], not a full date. The correct answer, and duplicate, is this one : http://stackoverflow.com/questions/18329631/enum-of-days-in-java-with-calendar – Alex R. Aug 17 '16 at 14:39
  • @Alexr See [my Answer](http://stackoverflow.com/a/32028420/642706) that I just now updated to `java.time.DayOfWeek`. And, no, your linked Question does not cover getting the name of day-of-week. – Basil Bourque Aug 17 '16 at 14:44
  • @AlexR. Update your Question to indicate Sunday is zero. Clarifications need to be in the Question, not the comments. And you can adjust between your numbers and the standard numbers by simply changing zero to seven and vice versa. The other six days need no adjustment. – Basil Bourque Aug 17 '16 at 18:31

3 Answers3

7

You can use the DayOfWeek enum to get a day by its number:

System.out.println(DayOfWeek.of(1).toString());

Output:

MONDAY
shmosel
  • 49,289
  • 6
  • 73
  • 138
4

Answer found thanks to shmosel. I am using the following :

DateFormatSymbols.ge‌tInstance().getWeekda‌​ys()
shmosel
  • 49,289
  • 6
  • 73
  • 138
Alex R.
  • 861
  • 1
  • 13
  • 28
1

You can simply Make Array of string days and get day by Integer using indexing.......

String[] strDays = new String[] { "Sunday", "Monday", "Tuesday","Wednesday", "Thursday","Friday", "Saturday" };

// Day_OF_WEEK starts from 1 while array index starts from 0
    System.out.println("Current day is : " + strDays[0])

OutPut:

Sunday
Evgenij Reznik
  • 17,916
  • 39
  • 104
  • 181