java中日期格式的转换和应用

时间:2023-03-09 22:23:40
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的子类,一般日期的格式化都是实例化该类实现

具体应用如下

package com.gree.java;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.SimpleFormatter; public class simpledate { public static void main(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=new Date();
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=new SimpleDateFormat(
"一年中的第 D 天 一年中第w个星期 一月中第W个星期 在一天中k时 z时区");
Date now=new Date();
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:27
2004年12月16日 17时24分27秒 星期四
一年中的第 351 天 一年中第51个星期 一月中第3个星期 在一天中17时 CST时区
16 Dec 2004 09:24:27 GMT
2004-12-16 17:24:27
Thu Dec 16 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=new ConnectDB();
PreparedStatement stmt=null;
Connection conn=null;
conn=db.getConnection();
String sql="update user set lastlogintime=? where name=?";
try { java.util.Date dates=new java.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();