关于程序中时间的一些操作处理

来源:互联网 发布:快速备案淘宝网 编辑:程序博客网 时间:2024/05/29 15:51

1、将一个时间转化为指定格式(oracle中)

TO_CHAR(SSS009,'yyyy-mm-dd') SSS009    //SSS009 格式可为 :2014/12/24 11:53:46

2、判断当前时间是否在一个区间内(oracle中)

SSS114 <=TO_CHAR(SYSDATE,'yyyy-mm-dd')    //SSS114格式为    2013-08-15
SSS115 >=TO_CHAR(SYSDATE,'yyyy-mm-dd')    //SSS115格式为     9999-12-31

(SYSDATE,'yyyy-mm-dd')   SYSDATE 取数据库中指定的时间     前后时间格式一致可以直接比较大小


3、java中比较时间大小(java中)

/**
* 比较日期大小  d1>d2 返回 1; d1=d2 返回 0;d1<d2 返回 -1。
* @param d1
* @param d2
* @return
* @author zwl
* @date 2014-12-9 下午12:34:01
*/
public static int compareTwoDate(Date d1,Date d2){
if(d1.after(d2)){
return 1;
}else if(d1.before(d2)){
return -1;
}else{
return 0;
}
}

/**
* 比较日期大小  d1>d2 返回 1; d1=d2 返回 0;d1<d2 返回 -1。
* @param d1
* @param d2
* @return
* @author zwl
* @throws BusinessException 
* @throws ParseException 
* @date 2014-12-9 下午12:34:01
*/
public static int compareTwoDate(String d1,String d2,String format) throws BusinessException{
try{
SimpleDateFormat sf=new SimpleDateFormat(format);
Date dd1=sf.parse(d1);
Date dd2=sf.parse(d2);

return compareTwoDate(dd1, dd2);
}catch(Exception e){
e.printStackTrace();
throw new BusinessException(e.getMessage());
}

}

0 0