本篇文章主要是通过连接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;
表中数据:
id | name | location | myTime |
---|---|---|---|
1 | Fern | China | 2016-08-08 21:43:25 |
2 | Lucy | America | 2016-08-08 22:43:20 |
3 | Ben | Austrilia | 2016-08-09 13:52:34 |
4 | Jack | Africa | 2016-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 {
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();
}
}
}
}
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));