/*** * 判斷字符串是否是HHmmss格式 * @param mes 字符串 * @return boolean 是否是日期格式 */ public static boolean isSjFormat(String mes){ if(mes.length()!=6){ return false; } String regex="^\\d+$"; if(!mes.matches(regex)){ return false; } int h; int m; int s; try { h=Integer.parseInt(mes.substring(0, 2)); m=Integer.parseInt(mes.substring(2,4)); s=Integer.parseInt(mes.substring(4, 6)); if(h>23||h<0||m>59||m<0||s>59||s<0){ return false; } } catch (Exception e) { LoggerUtil.info(mes+"不是HHmmss時間格式的字符串"); return false; } return true; }
/*** * 判斷字符串是否是yyyyMMddHHmmss格式 * @param mes 字符串 * @return boolean 是否是日期格式 */ public static boolean isRqSjFormat(String mes){ String format = "([0-9]{4})(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])" + "([01][0-9]|2[0-3])[0-5][0-9][0-5][0-9]"; Pattern pattern = Pattern.compile(format); Matcher matcher = pattern.matcher(mes); if (matcher.matches()) { pattern = Pattern.compile("(\\d{4})(\\d{2})(\\d{2}).*"); matcher = pattern.matcher(mes); if (matcher.matches()) { int y = Integer.valueOf(matcher.group(1)); int m = Integer.valueOf(matcher.group(2)); int d = Integer.valueOf(matcher.group(3)); if (d > 28) { Calendar c = Calendar.getInstance(); c.set(y, m-1, 1);
//每個月的最大天數 int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH); return (lastDay >= d); } } return true; } return false; }
/***
* 判斷字符串是否是yyyyMMdd格式
* @param mes 字符串
* @return boolean 是否是日期格式
*/
public static boolean isRqFormat(String mes){
String format = "([0-9]{4})(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])";
Pattern pattern = Pattern.compile(format);
Matcher matcher = pattern.matcher(mes);
if (matcher.matches()) {
pattern = Pattern.compile("(\\d{4})(\\d{2})(\\d{2})");
matcher = pattern.matcher(mes);
if (matcher.matches()) {
int y = Integer.valueOf(matcher.group(1));
int m = Integer.valueOf(matcher.group(2));
int d = Integer.valueOf(matcher.group(3));
if (d > 28) {
Calendar c = Calendar.getInstance();
c.set(y, m-1, 1); //每個月的最大天數
int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
return (lastDay >= d);
}
}
return true;
}
return false;
}