Get day string from day of week integer java [duplicate]

旧街凉风 提交于 2021-02-05 04:52:03

问题


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 ?


回答1:


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

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

Output:

MONDAY



回答2:


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

DateFormatSymbols.ge‌tInstance().getWeekda‌​ys()



回答3:


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


来源:https://stackoverflow.com/questions/38990952/get-day-string-from-day-of-week-integer-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!