java.sql.Timestamp;
java.sql.Time;
java.sql.Date;
用于数据库
使用 SimpleDateFormat 将数据库中的时间格式化
// 使用 SimpleDateFormat 将数据库中的时间格式化
public class TestDate {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/testjdbc?useUnicode=true&characterEncoding=UTF-8", "root", "root");
stmt = conn.createStatement();
rs = stmt.executeQuery("select lastLoginTime from t_user1");
while (rs.next()) {
java.sql.Date d = rs.getDate("lastLoginTime");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
System.out.println(sdf.format(d));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
使用 Calendar 将月份提取出来
// 使用 Calendar 将月份提取出来
public class TestDate1 {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/testjdbc?useUnicode=true&characterEncoding=UTF-8", "root", "root");
stmt = conn.createStatement();
rs = stmt.executeQuery("select lastLoginTime from t_user1");
while (rs.next()) {
Date d = rs.getDate("lastLoginTime");
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
// 初始化
Calendar c = Calendar.getInstance();
// c拿到了Date的时间了
c.setTime(d);
System.out.println(c.get(Calendar.MONTH));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
打印出来为 00:00:00 ,因为Date类型没有保存时间,它保存的是日期,所以打印出来为00:00:00
// 使用 Calendar 将月份提取出来
public class TestDate2 {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/testjdbc?useUnicode=true&characterEncoding=UTF-8", "root", "root");
stmt = conn.createStatement();
rs = stmt.executeQuery("select lastLoginTime from t_user1");
while (rs.next()) {
// Date d = rs.getDate("lastLoginTime");
// SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
// // 打印出来为 00:00:00 ,因为Date类型没有保存时间,它保存的是日期,所以打印出来为00:00:00
// System.out.println(sdf.format(d));
// 打印时间
Timestamp ts = rs.getTimestamp("lastLoginTime");
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
System.out.println(sdf.format(ts));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
拿到当前系统的时间
public class TestDateAndTime {
public static void main(String[] args) {
// 拿到当前系统的时间
// 可以用于程序的耗时计算(在程序执行的时候写一次,在程序结束的时候写一次,在两者相减)
System.out.println(System.currentTimeMillis());
Date d = new Date();
System.out.println("Date:"+d);
Calendar c = Calendar.getInstance();
System.out.println("当前年为:"+c.get(Calendar.YEAR));
// 将字符串转化成Date或者Time,使用Timestamp的valueOf()方法
String s = "1970-12-30 08:24:37.0";
Timestamp ts = Timestamp.valueOf(s);
System.out.println(ts);
}
}
获取日本当前的时间:TimeZone.getTimeZone(“Japan”)
//获取日本当前的时间:TimeZone.getTimeZone("Japan")
public class TestDateAndTime1 {
public static void main(String[] args) {
Calendar cJapan = new GregorianCalendar(TimeZone.getTimeZone("Japan"));
// HOUR_OF_DAY:24小时制的
System.out.println("日本当前的小时为:"+cJapan.get(Calendar.HOUR_OF_DAY));
// 获取打印 TimeZone下的ID
for(String id:TimeZone.getAvailableIDs()) {
System.out.println(id);
}
}
}