JAVA实现的酒店管理系统
源代码在线查看: 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 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;
}
}