如何用java判断一个字符串符合oracle中的date类型

phoenix_zg 2008-01-31 08:36:54
在insert语句中,对于date类型的字符串,发现"2008 12 12","2008/1212","200812/12"这样的也能插入,但是"081212"这样的就不行。我现在想用java去判断一个字符串是否符合oracle中的date类型,不用执行sql语句的方式,请教高手该如何来判断?
...全文
412 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
phoenix_zg 2008-02-04
  • 打赏
  • 举报
回复
我自己写的一个方法,如果有不对之处还请各位大虾指正:
private boolean checkDateType(String date) {
//2008/12/12
String pattern1 = "^[\\d]{1,4}([\\D|\\W]{1}[\\d]{1,2}){2}$";

//2008/1212
String pattern2 = "^[\\d]{1,4}[\\D|\\W]{1}[\\d]{4}$";

//200812/12
String pattern3 = "^[\\d]{6}[\\D|\\W]{1}[\\d]{1,2}$";

//20081212
String pattern4 = "^[\\d]{8}$";

if (date.matches(pattern1)) {
String[] ymd = date.split("[\\D|\\W]");
Calendar calendar = new GregorianCalendar(Integer.parseInt(ymd[0]), Integer.parseInt(ymd[1]) - 1,
Integer.parseInt(ymd[2]));
calendar.setLenient(false);
try {
calendar.get(Calendar.YEAR);
return true;
} catch (Exception e) {
return false;
}

} else if (date.matches(pattern2)) {
String[] ymd = date.split("[\\D|\\W]");
Calendar calendar = new GregorianCalendar(Integer.parseInt(ymd[0]), Integer.parseInt(ymd[1].substring(0, 2)) - 1,
Integer.parseInt(ymd[1].substring(2)));
calendar.setLenient(false);
try {
calendar.get(Calendar.YEAR);
return true;
} catch (Exception e) {
return false;
}

} else if (date.matches(pattern3)) {
String[] ymd = date.split("[\\D|\\W]");
Calendar calendar = new GregorianCalendar(Integer.parseInt(ymd[0].substring(0, 4)), Integer.parseInt(ymd[0].substring(4)) - 1,
Integer.parseInt(ymd[1]));
calendar.setLenient(false);
try {
calendar.get(Calendar.YEAR);
return true;
} catch (Exception e) {
return false;
}

} else if (date.matches(pattern4)) {

Calendar calendar = new GregorianCalendar(Integer.parseInt(date.substring(0, 4)),
Integer.parseInt(date.substring(4, 6)) - 1, Integer.parseInt(date.substring(6)));
calendar.setLenient(false);

try {
calendar.get(Calendar.YEAR);
return true;
} catch (Exception e) {
return false;
}
} else {
return false;
}


}
老紫竹 2008-02-01
  • 打赏
  • 举报
回复
楼主啊! 人都分不清楚,你还让机器去区分啊!

干脆你用正则表达式算了。
String str = "2000-02-01";
System.out.println(str.matches("^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$"));
phoenix_zg 2008-02-01
  • 打赏
  • 举报
回复
对于竹子的写法,如果是/2008/1212/ 这样的似乎也是正确的日期阿,似乎不对
phoenix_zg 2008-02-01
  • 打赏
  • 举报
回复
oracle中的date类型好像除了字母和数字都可以做分割符,比如2008!12!12 2008#12#12 都是可以插入到数据库中的,如果按照老紫竹的做法可就麻烦了,不知道有没有更好的办法啊,竹子!另外对于.为什么要转译阿,不是很明白这个,麻烦竹子解释下
moontrace 2008-02-01
  • 打赏
  • 举报
回复
加入包
import java.text.SimpleDateFormat;
import java.util.Date;
然后就可以进行操作,如:

Date dt = new Date();
SimpleDateFormat smpDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sTime = smpDateFormat.format(dt);

根据("yyyy-MM-dd HH:mm:ss")就可以确定格式,
2008/1212
这样的应该也可以。
老紫竹 2008-02-01
  • 打赏
  • 举报
回复
  private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
public static Date parse(String datestr) {
if (datestr == null) {
return null;
}
datestr = datestr.replaceAll(" ",""); // 去掉空格
datestr = datestr.replaceAll("/", ""); // 替换/
datestr = datestr.replaceAll("-", ""); // 替换-
datestr = datestr.replaceAll("\\.", ""); // 替换.
try {
return dateFormat.parse(datestr);
} catch (Exception ex) {
return null;
}
}


支持
20080201
2008-02-01
2008.02.01
2008/02/01
2008 02 01
dracularking 2008-02-01
  • 打赏
  • 举报
回复
不管分隔符多也好 格式多也好 总有个范围
归纳总结起来 每一种相近模式都判断一下 只要符合任意一种就是OK的
Johnson_Hong 2008-02-01
  • 打赏
  • 举报
回复
按照楼主的要求,要做到这个的话,你必须先知道oracle支持的所有日期格式。。。。。。,否则别的就别谈了
老紫竹 2008-02-01
  • 打赏
  • 举报
回复
更详细的正则表达式验证日期的内容,请看这里
http://www.java2000.net/viewthread.jsp?tid=471
ymcano11 2008-02-01
  • 打赏
  • 举报
回复
private boolean dealwith(String s){
try{
Date date = new Date(s);
return true;
}catch(Exception ex){
return false;
}
}
rabbitbug 2008-02-01
  • 打赏
  • 举报
回复
你在sql语句中直接把字形串转成date类型,最好加格式化
huchaofei 2008-02-01
  • 打赏
  • 举报
回复
SimpleDateFormat sim=new SimpleDateFormat("dd-MM月-yyyy");
try {
String s="日期字符串";
Date date=sim.parse(s);
} catch (Exception e) {
e.printStackTrace();
}
如果没有异常的话,那么s就是Oracle日期类型,否则就不是的。
phoenix_zg 2008-01-31
  • 打赏
  • 举报
回复
6楼的 对于“2008/1212”这个字符串在oracle中用insert语句可以正常插入的,是合法的日期,但是你用java去new能new出来吗。哪有那么简单阿
xjlz0001 2008-01-31
  • 打赏
  • 举报
回复
很简单,如果在java中,date可以通过string new出来,同时可以设置date的格式,所以,很简单,只要能new出来,就OK。
phoenix_zg 2008-01-31
  • 打赏
  • 举报
回复
能否说的具体点
ghostkngiht 2008-01-31
  • 打赏
  • 举报
回复
正则表达式应该能解决
guo0399 2008-01-31
  • 打赏
  • 举报
回复
看来理解错了,观注--------
phoenix_zg 2008-01-31
  • 打赏
  • 举报
回复
我说的是在java程序中,不是在pl/sql中,另外在java中不用insert之类的sql语句,用纯粹的java逻辑判断
guo0399 2008-01-31
  • 打赏
  • 举报
回复
直接在执行完语句之后写个Exception就可以了,如:
EXCEPTION
WHEN OTHERS THEN null;
这样,就会继续执行下面该执行的动作
phoenix_zg 2008-01-31
  • 打赏
  • 举报
回复
8楼的什么意思不懂
加载更多回复(1)

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧