`
陈静静2014
  • 浏览: 40788 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

oracle日期操作

阅读更多
多种日期格式:

YYYY:四位表示的年份
YYY,YY,Y:年份的最后三位、两位或一位,缺省为当前世纪
MM:01~12的月份编号
MONTH:九个字符表示的月份,右边用空格填补
MON:三位字符的月份缩写
WW:一年中的星期
D:星期中的第几天
DD:月份中的第几天
DDD:年所中的第几天
DAY:九个字符表示的天的全称,右边用空格补齐
HH,HH12:一天中的第几个小时,12进制表示法
HH24:一天中的第几个小时,取值为00~23
MI:一小时中的分钟
SS:一分钟中的秒
SSSS:从午夜开始过去的秒数

date
dateTime
sysdate 获得系统当前的日期和时间


to_date() 作用将字符类型按一定格式转化为日期类型:
    to_date('2004-11-27 13:34:43', 'yyyy-mm-dd hh24:mi:ss') 将得到具体的时间


to_char():将日期转按一定格式换成字符类型
       SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') time from dual;
            select to_char(sysdate,'yyyy-mm-dd') time from dual;
            select to_char(sysdate,'yy-mm') time from dual;   
            select to_char(sysdate,'mm-dd-yy') time from dual;       


         between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd')

例如:select * from tb where riqi between '2009-01-22 00:00:00' and '2009-01-22 23:59:59' 

插入日期:
  INSERT INTO emp(hiredate) VALUES ('10-1月-03');
  INSERT INTO emp(hiredate) VALUES (to_date('2004-11-27','yyyy-mm-dd'));

日期比较:
  select * from emp where hiredate < to_date('1981-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss')
  select * from up_date where update <= to_date('2007-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss')
  select * from up_date where update >= to_date('2007-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss')


在某段时间内:
select * from up_date where update between to_date('2007-07-07 00:00:00','yyyy-mm-dd hh24:mi:ss') and      to_date('2007-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss')
select * from up_date where update < to_date('2007-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss') and update >    to_date('2007-07-07 00:00:00','yyyy-mm-dd hh24:mi:ss')
select * from up_date where update < date1 and update > date2

在java代码中转化:

字符串转化为时间:
String brithday=new String("1991-02-02");
  SimpleDateFormat a=new SimpleDateFormat("yyyy-MM-dd");
  Date b=a.parse(brithday);
  System.out.println("将字符串转化为时间是:"+b);
时间转化为字符串
SimpleDateFormat   sdf   =   new   SimpleDateFormat( "yyyyMMDDHHMMSSmmm ");
System.out.println(sdf.format(new   Date()));


往数据中写入一个DATE数据类型的bdate:
1.(1)产生一个日历,并赋值。getInstance() 使用默认时区和语言环境获得一个日历。
Calendar c1=Calendar.getInstance();
c1.set(2013, 7, 28);
book.setBdate(c1.getTime());  //c1.getTime()是DATE类型的
(2)设置时间为系统当前时间 br.setTdate(new Date());
  2.用PrepareStatement写入到数据库中。其中java.sql.Date有时可以直接写成Date
ps.setDate(1,new java.sql.Date(book.getBdate().getTime()));
3.用SimpleDateFormat固定一个格式并输出
SimpleDateFormat a=new SimpleDateFormat("yyyy-MM-dd");
  System.out.println("将字符串转化为时间是:"+a.format(book.getBdate()));
从数据库中读出一个DATE数据类型的bdate:
1.在dao中设置日期:
book.setBdate(rs.getDate("bdate"));
2.用SimpleDateFormat固定一个格式并输出
SimpleDateFormat a=new SimpleDateFormat("yyyy-MM-dd");
  System.out.println("将字符串转化为时间是:"+a.format(book.getBdate()));
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics