add methon

This commit is contained in:
Looly
2022-03-24 10:47:03 +08:00
parent 163dff00bd
commit eb0cc1a503
3 changed files with 89 additions and 52 deletions

View File

@@ -1,5 +1,7 @@
package cn.hutool.cron.pattern.parser;
import cn.hutool.core.date.Month;
import cn.hutool.core.lang.Assert;
import cn.hutool.cron.CronException;
/**
@@ -10,11 +12,6 @@ import cn.hutool.cron.CronException;
*/
public class MonthValueParser extends AbsValueParser {
/**
* Months aliases.
*/
private static final String[] ALIASES = {"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"};
public MonthValueParser() {
super(1, 12);
}
@@ -31,16 +28,13 @@ public class MonthValueParser extends AbsValueParser {
/**
* 解析别名
*
* @param value 别名值
* @return 月份int值
* @param monthName 别名值
* @return 月份int值从1开始
* @throws CronException 无效月别名抛出此异常
*/
private int parseAlias(String value) throws CronException {
for (int i = 0; i < ALIASES.length; i++) {
if (ALIASES[i].equalsIgnoreCase(value)) {
return i + 1;
}
}
throw new CronException("Invalid month alias: {}", value);
private int parseAlias(String monthName) throws CronException {
final Month month = Month.of(monthName);
Assert.notNull(month, () -> new CronException("Invalid month alias: {}", monthName));
return month.getValueBaseOne();
}
}