⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 checkyearandmonth.java

📁 JAVA实现的酒店管理系统
💻 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 <= 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 + -