I'm modifying a calendar and there is a case where I'm given a month number and I need to compare it with a month name.
Of course, I could create a switch statement, but I was wondering if I can obtain the month name from the number or vice versa to do the conditional.
Thanks!
If you compare the int
number with Calendar
's month constants (ie, JANUARY
, AUGUST
, etc.), there won't be a problem with having the number instead of the name.
Here's a way to get the actual name, if you need it:
String getMonthForInt(int m) {
DateFormatSymbols dfs = new DateFormatSymbols();
String[] months = dfs.getMonths();
if (m >= 0 && m <= 11 ) {
return months[m];
}
return null;
}
Taken from here .
将月份名称存储在List<String>
,并使用list.indexOf(monthName)
查找其索引(数字从0开始),并使用list.get(index)
获取给定月份数字的名称(仍然从0)。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.