您的位置:首页 > 编程语言 > Java开发

Java的基础知识9——日期和时间

2016-08-09 16:43 525 查看
本篇文章主要是通过连接MySQL,然后从数据库中读取日期和时间。

1、准备工作

(1)创建数据库

create database test;


(2)使用数据库

use test;


(3)创建表

create table student(
id int primary key auto_increment,
name varchar(20),
location varchar(30),
myTime datetime
);


(4)插入数据

insert into student(id,name,location,myTime) values(null,'Fern','China',now());
insert into student(id,name,location,myTime) values(null,'Lucy','America',now());
insert into student(id,name,location,myTime) values(null,'Ben','Austrilia',now());
insert into student(id,name,location,myTime) values(null,'Jack','Afica',now());


(5)查看表中数据

select * from student;


表中数据:

idnamelocationmyTime
1FernChina2016-08-08 21:43:25
2LucyAmerica2016-08-08 22:43:20
3BenAustrilia2016-08-09 13:52:34
4JackAfrica2016-08-09 17:35:12

2、连接数据库并将得到的日期和时间格式化

(这里采用两种方法将得到的日期和时间格式化)

(1)

import java.sql.*;
import java.text.SimpleDateFormat;
//import java.util.Calendar;

public class Test {

public static void main(String[] args) {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
Timestamp ts = null;
//Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日   HH时mm分ss秒");
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager .getConnection("jdbc:mysql://localhost/caoyahong?user=root&password=root");
st = conn.createStatement();
rs = st.executeQuery("select myTime from student");
while (rs.next()) {
ts = rs.getTimestamp("myTime");
//c.setTime(date);
//System.out.println(c.get(Calendar.DATE));
System.out.println(sdf.format(ts));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (st != null) {
st.close();
st = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}


(2)

import java.sql.*;
import java.text.SimpleDateFormat;
//import java.util.Calendar;

public class Test1 {

public static void main(String[] args) {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
Date date = null;
Time date2 = null;
//Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
SimpleDateFormat sdf2 = new SimpleDateFormat("HH时mm分ss秒");
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager .getConnection("jdbc:mysql://localhost/caoyahong?user=root&password=root");
st = conn.createStatement();
rs = st.executeQuery("select myTime from student");
while (rs.next()) {
date = rs.getDate("myTime");
date2 = rs.getTime("myTime");
//c.setTime(date);
//System.out.println(c.get(Calendar.DATE));
System.out.print(sdf.format(date));
System.out.println("   "+sdf2.format(date2));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
t
4000
ry {
if (rs != null) {
rs.close();
rs = null;
}
if (st != null) {
st.close();
st = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}


3、得到当前时间的几种方法

(1)
System.currentTimeMillis();


可以使用这个方法测某个程序执行需要多少毫秒。例如;

public class Test {

public static void main(String[] args) {

long before = System.currentTimeMillis();
int result = 0;
for(int i=0;i<100000000;i++){
result = result + i;
}
long after = System.currentTimeMillis();
long last = after - before;
System.out.println(last);
}
}


(2)
Date d = new Date();


(3)

Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);//年
int month=c.get(Calendar.MONTH);//月
int day=c.get(Calendar.DATE);//日
int hour=c.get(Calendar.HOUR);//时
int minute=c.get(Calendar.MINUTE);//分
int second=c.get(Calendar.SECOND);//秒


4、将一个字符串转换为时间类型

import java.sql.Timestamp;
import java.util.Calendar;

public class Test {

public static void main(String[] args) {

String s = "2010-9-1 12:45:30";
Timestamp ts = Timestamp.valueOf(s);
//使用 JDBC 时间戳转义格式的 String 对象转换为 Timestamp 值。
Calendar c = Calendar.getInstance();
c.setTime(ts);
System.out.println(c.get(Calendar.YEAR));
System.out.println(ts);
}
}


5、对于时区的处理

Calendar cJapan = new GregorianCalendar(TimeZone.getTimeZone("Japan"));
System.out.println(cJapan.get(Calendar.HOUR_OF_DAY));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: