java 查询日期行列转换_java中日期格式的转换和应用

java中主要有3个类用于日期格式转换    DateFormat 、SimpleDateFormat、Calendar

SimpleDateFormat函数的继承关系:

java.lang.Object

|

+----java.text.Format

|

+----java.text.DateFormat

|

+----java.text.SimpleDateFormat

下面是个小例子:

import java.text.*;

import java.util.Date;

/**

SimpleDateFormat函数语法:

G 年代标志符

y 年

M 月

d 日

h 时 在上午或下午 (1~12)

H 时 在一天中 (0~23)

m 分

s 秒

S 毫秒

E 星期

D 一年中的第几天

F 一月中第几个星期几

w 一年中第几个星期

W 一月中第几个星期

a 上午 / 下午 标记符

k 时 在一天中 (1~24)

K 时 在上午或下午 (0~11)

z 时区

1.SimpleDateFormat

该类是DateFormat的子类,一般日期的格式化都是实例化该类实现

具体应用如下

packagecom.gree.java;importjava.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.Date;importjava.util.logging.SimpleFormatter;public classsimpledate {public static voidmain(String [] args){

SimpleDateFormat a= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//全日期格式,24小时制

SimpleDateFormat b= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");//全日期格式,12小时制

SimpleDateFormat c=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//获取5天以后的日期

SimpleDateFormat d=new SimpleDateFormat("yyyy-MM-dd");//获取年月日格式

SimpleDateFormat e=new SimpleDateFormat("yyyy");//获取年份

SimpleDateFormat f=new SimpleDateFormat("MM");//获取月份

SimpleDateFormat g=new SimpleDateFormat("dd");//获取天

Calendar calendar=Calendar.getInstance();

calendar.add(Calendar.DATE,5);

Date date1=newDate();

Date date=calendar.getTime();

System.out.println(a.format(date));

System.out.println(b.format(date1));

System.out.println(c.format(date1));

System.out.println(d.format(date1));

System.out.println(e.format(date1));

System.out.println(f.format(date1));

System.out.println(g.format(date1));//System.out.println(g.format(date1));}

}

结果如下:

2014-11-18 16:36:30

2014-11-13 04:36:30 下午

2014-11-13 16:36:30

2014-11-13

2014

11

13

SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");

SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm");

SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//等价于now.toLocaleString()

SimpleDateFormat myFmt3=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E ");

SimpleDateFormat myFmt4=newSimpleDateFormat("一年中的第 D 天 一年中第w个星期 一月中第W个星期 在一天中k时 z时区");

Date now=newDate();

System.out.println(myFmt.format(now));

System.out.println(myFmt1.format(now));

System.out.println(myFmt2.format(now));

System.out.println(myFmt3.format(now));

System.out.println(myFmt4.format(now));

System.out.println(now.toGMTString());

System.out.println(now.toLocaleString());

System.out.println(now.toString());

效果:

2004年12月16日 17时24分27秒04/12/16 17:24

2004-12-16 17:24:272004年12月16日 17时24分27秒 星期四

一年中的第351天 一年中第51个星期 一月中第3个星期 在一天中17时 CST时区16 Dec 2004 09:24:27GMT2004-12-16 17:24:27Thu Dec16 17:24:27 CST 2004

oracle日期格式转换:

由String类型转换为date类型:date可以为年月日,也可以是年月日时分秒

insert into table(date) values(to_date(?,'YYYY-MM-DD HH24-MI-SS'))或insert into table(date) values(to_date(?,'YYYY-MM-DD'))

例:

insert into timod400(gzbh,sqdt) values('CK301/0058',to_date('2014-12-14','YYYY-MM-DD'))

insert into timod400(gzbh,sqdt) values('CK301/0058',to_date('2014-12-14 22:55:08','YYYY-MM-DD HH24:mi:ss'))

由date类型转换为String类型:

select gzbh,gznm,jjcd, sqbm, sqnm,zsyy,to_char(sqdt,'YYYY-MM-DD') sqdt from timod400

select to_char(t.sqdt,'YYYY-MM-DD HH24:mi:ss') sqdt from timod400 t

插入系统当前时间:

insert into timod400(gzbh,sqdt) values('fsdf213213',sysdate)

mysql日期格式转换:

由date类型转换为String类型

SimpleDateFormat de=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS");

//SimpleDateFormat de=new SimpleDateFormat("yyyy-MM-dd");只包含年月日的格式

Timestamp restime=rs.getTimestamp("registertime");

Timestamp logintime=rs.getTimestamp("lastlogintime");if(restime!=null){

String registertime=de.format(restime);

}//SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");

if(logintime!=null){

String lastlogintime=de.format(logintime);

}

由String格式转换为date格式:

1.插入系统当前时间:

DB服务器:insert into table(name,makedate) values('ceshi',NOW());

客户端:插入new Date()

代码:

ConnectDB db=newConnectDB();

PreparedStatement stmt=null;

Connection conn=null;

conn=db.getConnection();

String sql="update user set lastlogintime=? where name=?";try{

java.util.Date dates=newjava.util.Date();

Date date=new Date(dates.getTime()); //Date类为java。sql.Date类

stmt=conn.prepareStatement(sql);

stmt.setDate(1,date );

stmt.setString(2, "wcs");

stmt.executeUpdate();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值