JAVA实现的酒店管理系统
源代码在线查看: checkyearandmonth.java
package tools;
/*
* @Author:黄顺武
* Create Time:2008-2-22
* Description:判断用户输入的年月格式是否符合规范
*/
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
public class CheckYearAndMonth {
private static int year = -1;
private static int month = -1;
public CheckYearAndMonth() {
}
public static 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;
if (year != -1) {
year = -1;// 重新初始化,解决在同一个类中调用时不能被多次初始化的问题
}
if (month != -1) {
month = -1;// 重新初始化,解决在同一个类中调用时不能被多次初始化的问题
}
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;
}
}