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

本篇文章主要是通过连接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 {
            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));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值