java中时间类型转换_java中时间类型转换

在java中有六大时间类,分别是:

1、java.util包下的Date类,

2、java.sql包下的Date类,

3、java.text包下的DateFormat类,(抽象类)

4、java.text包下的SimpleDateFormat类,

5、java.util包下的Calendar类,(抽象类)

6、java.sql包下的Time类,

7、java.sql包下的TimeStamp类。

一、java.util包下的Date类:

Date主要用来生成时间,Date有两个构造方法:Date()和Date(long date)

80930c5dcf4e99bacda309dd6173cda9.png

206afe29e9a10a8c7a95a93748613586.png

二、java.text包下的DateFormat类,(抽象类):

DateFormat继承自Format。

9168c5cc75a85648c868fa8c9a48278c.png

87ce0e6cb6bd881d0472707930eef60c.png

下面两个是实现的接口,两个接口无任何内容:

c8c1dc6786f6fd4036c1d1ebbe7d2ad3.png

04bec3dcdae9d593deefb8376ef4bbed.png

DateFormat是时间/日期格式化子类的抽象类,所以不能有构造方法实例化,可以用两个静态函数进行实例化。

getDateInstance()------返回的是日期

getDateTimeInstance()--------------------返回的是时间+日期。

三、java.text包下的SimpleDateFormat类:

SimpleDateFormat继承自DateFormat类,

主要功能是:完成日期之间的格式转换。

yyyy:MM:dd,HH:mm:ss:SSS(SSS是毫秒数)

fd8453df5ca832621174ee93930f305d.png

四、java.util包下的Calendar类,(抽象类):

此类实例化有两种方式:

1、Calendar time=new GregorianCalendar();

2、Calendar time =Calendar.getInstance();

6c4f7c2157fbcf9dd05d0743ae4d0bf8.png

此类和日历相关:“YEAR”年,“MONTH”月,“DAY_OF_MONTH”日,"DAY_OF_WEEK"星期,"HOUR"小时。如:

f3f26bdb992f1c8c31facfbe21382a0c.png

五、java.sql包下的Date类:

只针对SQL语句使用,Date date=new Date();(也就=2017-01-01,没有时间部分)

六、java.sql包下的Time类,

七、java.sql包下的TimeStamp类。

c8e47e3a02bfde649db35a880208b63e.png

5fa5ccdd4a126d657c8ed82942940dae.png

输出结果:

84d2994548ff1e4e30c3cd62b723ddff.png

4d9b192bf03a474faf38b871e415a15b.png

输出结果:

d57e7bb8c53bccbf3c4a3fdbbc0ba21a.png

6088fad930eb2711a432fb845bee9944.png

结果:

0842e2a109ddf029264cb8c97fc9c7e7.png

5c4b187f4f2a0f8b914c7f3d8267436b.png

结果:

32b21defa927cc5d37de0609cc523e51.png

27e9ce828f7dab962c8512dd471b0c0e.png

5e8efe7a8c4a903079bf847fd9d01f84.png

61d47407684c1d057222532431324cb0.png

结果:

314f9313128d27e76c8edc2a9be085ef.png

1 packagetest;2

3 importjava.text.DateFormat;4 importjava.text.ParseException;5 importjava.text.SimpleDateFormat;6 importjava.util.Calendar;7 importjava.util.Date;8 importjava.util.GregorianCalendar;9

10 public classtime {11 public static void main(String[] args) throwsParseException {12 //将当前日期对象转换成毫秒值

13 Date date1 = newDate();14 Long time1 =date1.getTime();15 Long time2 = System.currentTimeMillis();//获取当前时间的毫秒值

16 Calendar cal=Calendar.getInstance();17 Long caltime=cal.getTimeInMillis();18 System.out.println("当前日期对象转换成毫秒值:" + time2+" Calendar类日期转换成毫秒值:"+caltime);19

20 //将毫秒值转换成日期对象

21 Date date2 = newDate();22 Long time3 =System.currentTimeMillis();23 date2.setTime(time3);24

25 //将日期字符串转换成日期对象

26 DateFormat df1 = new SimpleDateFormat("yyyy/MM/dd");27 Date date3 = df1.parse("2017/01/12");28 System.out.println("日期字符串转换成日期对象:" +date3);29

30 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");31 Date date = df.parse("2009-06-12 02:06:37");32 System.out.println("日期字符串转换成日期格式:" +df.format(date));33

34 //将日期对象转换成日期字符串

35 Date datetime = newDate();36 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");37 String stringtime =sdf.format(datetime);38 System.out.println("日期对象转换成日期字符串:" +stringtime);39

40 Date date4 = newDate();41 DateFormat df41 =DateFormat.getDateInstance(DateFormat.LONG);42 DateFormat df42 =DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);43 String time4 =df41.format(date4);44 String time5 =df42.format(date4);45 System.out.println("getDateInstance时间:" + time4 + " getDateTimeInstance时间:" +time5);46

47 Date d = newDate();48 DateFormat df43 =DateFormat.getDateInstance(DateFormat.FULL);49 DateFormat df44 =DateFormat.getDateInstance(DateFormat.SHORT);50 DateFormat df45 =DateFormat.getDateInstance(DateFormat.MEDIUM);51 String time43 =df43.format(d);52 String time44 =df44.format(d);53 String time45 =df45.format(d);54 System.out.println("FULL类型时间:" +time43);55 System.out.println("SHORT类型时间:" +time44);56 System.out.println("MEDIUM类型时间:" +time45);57

58 //日历

59 Calendar time =Calendar.getInstance();60 int year =time.get(Calendar.YEAR);61 int month =time.get(Calendar.MONTH);62 int day =time.get(Calendar.DAY_OF_MONTH);63 int week =time.get(Calendar.DAY_OF_WEEK);64 int hour =time.get(Calendar.HOUR);65 System.out.println("当前时间是:" + year + "年" + month + "月" + day + "日,星期" + week + "," + hour + "点");66 }67 }

1 当前日期对象转换成毫秒值:1504089815431 Calendar类日期转换成毫秒值:1504089815441

2 日期字符串转换成日期对象:Thu Jan 12 00:00:00 CST 2017

3 日期字符串转换成日期格式:2009-06-12 02:06:37

4 日期对象转换成日期字符串:2017/08/30

5 getDateInstance时间:2017年8月30日 getDateTimeInstance时间:2017年8月30日 下午06时43分35秒6 FULL类型时间:2017年8月30日 星期三7 SHORT类型时间:17-8-30

8 MEDIUM类型时间:2017-8-30

9 当前时间是:2017年7月30日,星期4,6点

原文:http://www.cnblogs.com/whx20100101/p/7454784.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在MySQL时间类型有多种,包括DATETIME、DATE、TIME、TIMESTAMP等,不同的时间类型在Java的转换方式也略有不同。 1. DATETIME类型 在Java,可以使用java.sql.Timestamp类型来表示DATETIME类型。在通过JDBC从MySQL读取DATETIME类型的值时,可以使用ResultSet类的getTimestamp方法获取java.sql.Timestamp类型的值: ```java ResultSet rs = stmt.executeQuery("SELECT datetime_column FROM table"); while (rs.next()) { Timestamp datetime = rs.getTimestamp("datetime_column"); // ... } ``` 在将java.sql.Timestamp类型的值写入到MySQL的DATETIME类型的字段时,可以使用PreparedStatement类的setTimestamp方法: ```java PreparedStatement pstmt = conn.prepareStatement("INSERT INTO table (datetime_column) VALUES (?)"); Timestamp datetime = new Timestamp(System.currentTimeMillis()); pstmt.setTimestamp(1, datetime); pstmt.executeUpdate(); ``` 2. DATE类型 在Java,可以使用java.sql.Date类型来表示DATE类型。在通过JDBC从MySQL读取DATE类型的值时,可以使用ResultSet类的getDate方法获取java.sql.Date类型的值: ```java ResultSet rs = stmt.executeQuery("SELECT date_column FROM table"); while (rs.next()) { Date date = rs.getDate("date_column"); // ... } ``` 在将java.sql.Date类型的值写入到MySQL的DATE类型的字段时,可以使用PreparedStatement类的setDate方法: ```java PreparedStatement pstmt = conn.prepareStatement("INSERT INTO table (date_column) VALUES (?)"); Date date = new Date(System.currentTimeMillis()); pstmt.setDate(1, date); pstmt.executeUpdate(); ``` 3. TIME类型 在Java,可以使用java.sql.Time类型来表示TIME类型。在通过JDBC从MySQL读取TIME类型的值时,可以使用ResultSet类的getTime方法获取java.sql.Time类型的值: ```java ResultSet rs = stmt.executeQuery("SELECT time_column FROM table"); while (rs.next()) { Time time = rs.getTime("time_column"); // ... } ``` 在将java.sql.Time类型的值写入到MySQL的TIME类型的字段时,可以使用PreparedStatement类的setTime方法: ```java PreparedStatement pstmt = conn.prepareStatement("INSERT INTO table (time_column) VALUES (?)"); Time time = new Time(System.currentTimeMillis()); pstmt.setTime(1, time); pstmt.executeUpdate(); ``` 4. TIMESTAMP类型 在Java,可以使用java.sql.Timestamp类型来表示TIMESTAMP类型。在通过JDBC从MySQL读取TIMESTAMP类型的值时,可以使用ResultSet类的getTimestamp方法获取java.sql.Timestamp类型的值: ```java ResultSet rs = stmt.executeQuery("SELECT timestamp_column FROM table"); while (rs.next()) { Timestamp timestamp = rs.getTimestamp("timestamp_column"); // ... } ``` 在将java.sql.Timestamp类型的值写入到MySQL的TIMESTAMP类型的字段时,可以使用PreparedStatement类的setTimestamp方法: ```java PreparedStatement pstmt = conn.prepareStatement("INSERT INTO table (timestamp_column) VALUES (?)"); Timestamp timestamp = new Timestamp(System.currentTimeMillis()); pstmt.setTimestamp(1, timestamp); pstmt.executeUpdate(); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值