字符串的问题??还请指教!! SimpleDateFormat sdf = new SimpleDateFormat("yyyy");try {sdf.parse("199a");} catch (ParseException e) {System.out.println("年份格式不对");} 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 把字符串截成“年份”,“月份”,然后转换为int形,然后在判断是否争取 上面是年,这是月份,10月以下的自己先补0再检查。SimpleDateFormat sdf = new SimpleDateFormat("MM");try {sdf.parse("11");} catch (ParseException e) {System.out.println("年份格式不对");} 用SimpleDateFormat判断是最简单得; 用SimpleDateFormat不能检查月份,试试new SimpleDateFormat("MM").parse("14")就知道了。转成int再进行判断。try{ int year = Integer.parseInt("123a");}catch(NumberFormatException e){ System.out.println("年份格式不对");}try{ int month = Integer.parseInt("13"); if(month < 1 || month > 12) System.out.println("月份格式不对");}catch(NumberFormatException e){ System.out.println("月份格式不对");} //strDate 格式为:"0000-00-00"public boolean isAllowDate(String strDate) { if (strDate == null || strDate.trim().length() == 0) return true; if (strDate.trim().length() != 10) return false; for (int i = 0; i < 10; i++) { char c = strDate.trim().charAt(i); if (i == 4 || i == 7) { if (c != '-') return false; } else if (c < '0' || c > '9') return false; } int year = Integer.parseInt(strDate.trim().substring(0, 4)); int month = Integer.parseInt(strDate.trim().substring(5, 7)); if (month < 1 || month > 12) return false; int day = Integer.parseInt(strDate.trim().substring(8, 10)); int MONTH_LENGTH[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int LEAP_MONTH_LENGTH[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int daymax = isLeapYear(year) ? LEAP_MONTH_LENGTH[month - 1] : MONTH_LENGTH[month - 1]; if (day < 1 || day > daymax) return false; return true;}public boolean isLeapYear(int year) { if ((year % 4 == 0) && (year % 100 != 0 || year % 400 == 0)) return true; else return false;} 同志们,这样的判断还是放在 用户端判断把……<script language="JavaScript">// *****************************************************************************// 得到 Form 中的 Element;// ** 由于输入框以及其他Input的名字在 JavaScript 语法有冲突,所以只能通过这种方法来获得其 Handle// ** 相当于: document.all.[sysConfig.message]function CheckYearValue (theYearValue){ var theChar; var Result = ""; for (var i=0; i<theYearValue.length; i++) { theChar = theYearValue.charAt(i); if (theChar <'0' || theChar >'9') { Result = Result + "\r\n" + theChar; } } // 俺只判断了输入是否是数字…… 其他的判断可以自己加了…… return Result;}function CheckInput (IsNotifyUserWhenDataIsCorrect){ // for User Interface var Result = ""; // var ErrorCount = 0; // 检测输入的日期格式是否正确…… TempResult = CheckYearValue (document.all.beginYear.value); if (TempResult.length > 0) { ErrorCount ++; Result = Result + "\r\n" + ErrorCount + ":开始年份有错误的字符:" + TempResult; } TempResult = CheckYearValue (document.all.endYear.value); if (TempResult.length > 0) { ErrorCount ++; Result = Result + "\r\n" + ErrorCount + ":结束年份有错误的字符:" + TempResult; } // for User Interface... if (Result.length>0) { alert ("您的输入有 " + ErrorCount + " 个错误:\r\n\r\n" + Result); return false; } else { if (IsNotifyUserWhenDataIsCorrect) alert ("输入正确。"); return true; }}</script> java运行时会栈溢出这是为什么啊 验证码如何再次生成 ibatis3如何对select不使用缓存 还问窗体中JPanel切换的问题..... 关于 字符串的动态截取问题! 哪位高人知道JAVA里面怎么做打印,急!!!!1 C#转JAVA-up有分! 请问架设jsp服务器去哪里咨询比较好? 数据发送的方式 Eclipse 写java程序时错误: 找不到或无法加载主类 PrimaryVersion.Demo9_1 向大虾请教一个问题 如何用JDBC-ODBC桥连接sql server数据库?(在线)
try {
sdf.parse("11");
} catch (ParseException e) {
System.out.println("年份格式不对");
}
{
int year = Integer.parseInt("123a");
}
catch(NumberFormatException e)
{
System.out.println("年份格式不对");
}try
{
int month = Integer.parseInt("13");
if(month < 1 || month > 12)
System.out.println("月份格式不对");
}
catch(NumberFormatException e)
{
System.out.println("月份格式不对");
}
public boolean isAllowDate(String strDate) {
if (strDate == null || strDate.trim().length() == 0)
return true;
if (strDate.trim().length() != 10)
return false;
for (int i = 0; i < 10; i++) {
char c = strDate.trim().charAt(i);
if (i == 4 || i == 7) {
if (c != '-')
return false;
} else
if (c < '0' || c > '9')
return false;
}
int year = Integer.parseInt(strDate.trim().substring(0, 4));
int month = Integer.parseInt(strDate.trim().substring(5, 7));
if (month < 1 || month > 12)
return false;
int day = Integer.parseInt(strDate.trim().substring(8, 10));
int MONTH_LENGTH[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int LEAP_MONTH_LENGTH[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int daymax = isLeapYear(year) ? LEAP_MONTH_LENGTH[month - 1] : MONTH_LENGTH[month - 1];
if (day < 1 || day > daymax)
return false;
return true;
}
public boolean isLeapYear(int year) {
if ((year % 4 == 0) && (year % 100 != 0 || year % 400 == 0))
return true;
else
return false;
}
// *****************************************************************************
// 得到 Form 中的 Element;
// ** 由于输入框以及其他Input的名字在 JavaScript 语法有冲突,所以只能通过这种方法来获得其 Handle
// ** 相当于: document.all.[sysConfig.message]function CheckYearValue (theYearValue)
{
var theChar;
var Result = "";
for (var i=0; i<theYearValue.length; i++)
{
theChar = theYearValue.charAt(i);
if (theChar <'0' || theChar >'9')
{
Result = Result + "\r\n" + theChar;
}
}
// 俺只判断了输入是否是数字…… 其他的判断可以自己加了……
return Result;
}function CheckInput (IsNotifyUserWhenDataIsCorrect)
{
// for User Interface
var Result = ""; //
var ErrorCount = 0; // 检测输入的日期格式是否正确……
TempResult = CheckYearValue (document.all.beginYear.value);
if (TempResult.length > 0)
{
ErrorCount ++;
Result = Result + "\r\n" + ErrorCount + ":开始年份有错误的字符:" + TempResult;
}
TempResult = CheckYearValue (document.all.endYear.value);
if (TempResult.length > 0)
{
ErrorCount ++;
Result = Result + "\r\n" + ErrorCount + ":结束年份有错误的字符:" + TempResult;
} // for User Interface...
if (Result.length>0)
{
alert ("您的输入有 " + ErrorCount + " 个错误:\r\n\r\n" + Result);
return false;
}
else
{
if (IsNotifyUserWhenDataIsCorrect)
alert ("输入正确。");
return true;
}
}
</script>