📄 checkyearmonth.java
字号:
package file1;
/*
* @Author:黄顺武
* Description:判断用户输入的年月格式是否符合规范
*/
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
public class CheckYearMonth {
private int year = -1;
private int month = -1;
public CheckYearMonth() {
}
public String check(String value) {
try {
if (value.indexOf("-") == -1) {
JOptionPane.showMessageDialog(null, "年月必须为如:2007-09的格式!", "",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
StringTokenizer st = new StringTokenizer(value, "-");
if (st.countTokens() != 2) {
JOptionPane.showMessageDialog(null, "年月必须为如:2007-09的格式!", "",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
int count = 0;
while (st.hasMoreTokens()) {
count++;
if (count == 1) {
year = Integer.valueOf(st.nextToken());
} else if (count == 2) {
month = Integer.valueOf(st.nextToken());
break;
}
}
if (year <= 0 || month <= 0 || month > 12) {
JOptionPane.showMessageDialog(null, "年月必须为如:2007-09的格式!", "",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null, "年月必须为如:2007-09的格式!", "",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
return value;
}
public int getYear() {
return year;
}
public int getMonth() {
return month;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -