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();
總結
以上是生活随笔為你收集整理的java 查询日期行列转换_java中日期格式的转换和应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DNF100级刹那永恒技能加点详情一览
- 下一篇: java jdbc修改_java----