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

📄 getdatefromdatestr.java

📁 JAVA实现的酒店管理系统
💻 JAVA
字号:
package tools;

/*
 * @Author:黄顺武
 * Create Time:2008-2-22
 * Description:把一般的日期字符串转化为指定格式的日期值并返回
 */
import java.util.Calendar;
import java.util.Date;
import java.util.StringTokenizer;

public class GetDateFromDateStr {

	private static int year = -1;
	private static int month = -1;
	private static int dateOfMonth = -1;
	private static String commonDivider = "-";// 自定义的默认的分隔符

	public GetDateFromDateStr() {

	}

	public static Date getDate(String date) {
		try {
			reInitialized();// 调用自定义函数,手动初始化类常量成员数据
			return getDate(date, commonDivider);
		} catch (Exception e) {
			return null;
		}

	}

	public static Date getDate(String date, String divider) {

		try {
			reInitialized();// 调用自定义函数,调用自定义函数,手动初始化类常量成员数据
			StringTokenizer st = new StringTokenizer(date, divider);
			return theCommonPart(st);
		} catch (Exception e) {
			return null;
		}
	}

	public static Date theCommonPart(StringTokenizer st) {
		int count = 0;
		int test = 0;
		while (st.hasMoreTokens()) {
			switch (count) {
			case 0:
				year = Integer.valueOf(st.nextToken().trim());
				count++;
				test++;
				break;
			case 1:
				month = Integer.valueOf(st.nextToken().trim());
				count++;
				test++;
				break;
			case 2:
				dateOfMonth = Integer.valueOf(st.nextToken().trim());
				test++;
				break;
			default:
				break;
			}
		}
		if (test != 3) {
			return null;
		}
		if (year < 0) {
			return null;
		}
		if (month < 0 || month > 12) {
			return null;
		}
		if (dateOfMonth < 0 || dateOfMonth > 31) {
			return null;
		}
		Calendar c = Calendar.getInstance();
		c.set(year, month - 1, dateOfMonth);
		return c.getTime();
	}

	public int getYear() {
		return year;
	}

	public int getMonth() {
		return month;
	}

	public int getDateOfMonth() {
		return dateOfMonth;
	}

	private static void reInitialized() {// 重新初始化
		if (year != -1) {
			year = -1;
		}
		if (month != -1) {
			month = -1;
		}
		if (dateOfMonth != -1) {
			dateOfMonth = -1;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -