1.mysql数据库中有这样一个表:

mysql> select * from test_table;
+----------+---------------------+
| username | date |
+----------+---------------------+
| chengyu | 1990-10-04 00:00:00 |
| chengpei | 1980-09-12 12:23:01 |
+----------+---------------------+

其中date字段是datetime类型的;从数据库中将date字段取出来:

public static void main(String[] args) throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_demo?user=root&password=root");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from test_table");
while(rs.next()){
Date date = rs.getDate("date");
System.out.println(date);
}
}

Date取出来是java.sql.Date类型的;打印但是Date的toString()方法;显示如下:

1990-10-04
1980-09-12

现在将date取出来,转化为字符串,再次打印出来:

public static void main(String[] args) throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_demo?user=root&password=root");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from test_table");
while(rs.next()){
Date date = rs.getDate("date");
String date2 = new SimpleDateFormat("yyyy年MM月dd日").format(date);
System.out.println(date2);
}
}

打印结果如下:

1990年10月04日
1980年09月12日

2.从数据库中取出日期和时间:

在数据库中有这样的表:

public static void main(String[] args) throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_demo?user=root&password=root");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from test_table");
while(rs.next()){
/*
* java.sql.Date只能精确到年月日; Date类型只能够代表日期;
* 这里采用java.sql.Timestamp;
* 要取出毫秒来,创建数据库字段date timestamp(3) 意思是保留3为毫秒数
*/
Timestamp date = rs.getTimestamp("date");
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
//拿到年份:
int year = calendar.get(Calendar.YEAR);
//拿到月份:
int month = calendar.get(Calendar.MONTH) + 1;
//拿到日:
int day = calendar.get(Calendar.DAY_OF_MONTH);
//拿到时:
int hour_24 = calendar.get(Calendar.HOUR_OF_DAY);
int hour_12 = calendar.get(Calendar.HOUR);
//拿到分:
int minute = calendar.get(Calendar.MINUTE);
//拿到秒:
int second = calendar.get(Calendar.SECOND);
//拿到毫秒:
int millisecond = calendar.get(Calendar.MILLISECOND); System.out.println("日期和时间:"+ date);
System.out.println("年: " + year);
System.out.println("月: " + month);
System.out.println("日: " + day);
System.out.println("时(24): " + hour_24);
System.out.println("时(12): " + hour_12);
System.out.println("分: " + minute);
System.out.println("秒: " + second);
System.out.println("毫秒: " + millisecond);
}
}

打印如下:

日期和时间:1990-10-04 23:30:55.86
年: 1990
月: 10
日: 4
时(24): 23
时(12): 11
分: 30
秒: 55
毫秒: 860

2)resultSet.getTimestamp("date")后,将这个时间转化为String:

数据库中已存在的数据:

public static void main(String[] args) throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_demo?user=root&password=root");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select date from test_table");
while(rs.next()){
Timestamp timestamp = rs.getTimestamp("date");
String strdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(timestamp);
System.out.println(strdate);
}
}

打印出来:

1990-10-04 00:00:00.000
1989-10-04 23:59:55.086

 

3)将一个String类型的字符串存进数据库, 以java.sql.Date类型存进去:

public static void main(String[] args) throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_demo?user=root&password=root");
String sql = "insert into test_table values(?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
/*
* 将一个String类型的时间存储到数据库
* java.sql.Date和java.util.Date之间可以互相转化
* new SimpleDateFormat("yyyy-MM-dd hh:mm:ss") HH-24小时 hh-12小时
* 不过插入数据库后,由于是sql Date类型的,只能存储日期,就没有时间
*/
String strDate = "1990-10-04 23:30:55.86";
java.util.Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(strDate);
java.sql.Date date2 = new java.sql.Date(date.getTime());
pstmt.setString(1, "cy");
pstmt.setDate(2, date2);
pstmt.executeUpdate();
}

数据库显示:

4)将String类型的。连带时间分钟。毫秒一起存进数据库:

public static void main(String[] args) throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_demo?user=root&password=root");
String sql = "insert into test_table values(?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
/*
* java.sql.Timestamp/java.sql.Date都是 java.util.Date的子类;
*/
String strDate = "1989-10-04 23:59:55.86";
java.util.Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(strDate);
Timestamp date2 = new Timestamp(date.getTime());
pstmt.setString(1, "cp");
pstmt.setTimestamp(2, date2);
pstmt.executeUpdate();
}

  

java日期处理SimpleDateFormat等的更多相关文章

  1. Java日期转换SimpleDateFormat格式大全(转)

    24小时制时间显示: public class Datetime { public static void main(String args[]){ java.util.Date current=ne ...

  2. java 日期格式化-- SimpleDateFormat 的使用。字符串转日期,日期转字符串

    日期和时间格式由 日期和时间模式字符串 指定.在 日期和时间模式字符串 中,未加引号的字母 'A' 到 'Z' 和 'a' 到 'z' 被解释为模式字母,用来表示日期或时间字符串元素.文本可以使用单引 ...

  3. java日期格式大全 format SimpleDateFormat(转)

    java日期格式大全 format SimpleDateFormat   /**    * 字符串转换为java.util.Date<br>    * 支持格式为 yyyy.MM.dd G ...

  4. java中使用SimpleDateFormat实现字符串和日期的相互转换

    java中使用SimpleDateFormat实现字符串和日期的相互转换 import java.text.ParseException; import java.text.SimpleDateFor ...

  5. [转]Java日期时间使用总结

    原文地址:http://lavasoft.blog.51cto.com/62575/52975/ 一.Java中的日期概述   日期在Java中是一块非常复杂的内容,对于一个日期在不同的语言国别环境中 ...

  6. java日期操作大全

    摘自(http://www.blogjava.net/i369/articles/83483.html) java日期操作 大全 先来一个:  取得指定月份的第一天与取得指定月份的最后一天  http ...

  7. java日期处理总结

    Java日期时间使用总结   一.Java中的日期概述   日期在Java中是一块非常复杂的内容,对于一个日期在不同的语言国别环境中,日期的国际化,日期和时间之间的转换,日期的加减运算,日期的展示格式 ...

  8. Java日期时间使用总结

    一.Java中的日期概述   日期在Java中是一块非常复杂的内容,对于一个日期在不同的语言国别环境中,日期的国际化,日期和时间之间的转换,日期的加减运算,日期的展示格式都是非常复杂的问题.   在J ...

  9. Java 日期时间

    Java 日期时间 标签 : Java基础 Date java.util.Date对象表示一个精确到毫秒的瞬间; 但由于Date从JDK1.0起就开始存在了,历史悠久,而且功能强大(既包含日期,也包含 ...

随机推荐

  1. SELF, self in CORE DATA

    Predicate SELF Represents the object being evaluated. CORE DATA Retrieving Specific Objects If your ...

  2. 使用if else if else 统计计算

    package review20140419;/* * 统计一个班级的成绩,并统计优良中差和不及格同学个数以及求平均分 */public class Test2 {    //程序的入口    pub ...

  3. How to do code coverage test for windows service

    First, instrument the exe or dll by command vsinstr -coverage the dll/exe second, start the performa ...

  4. Codeforces Round #381 (Div. 2) A B C 水 构造

    A. Alyona and copybooks time limit per test 1 second memory limit per test 256 megabytes input stand ...

  5. ab中文手册

    名兒 NAME ab - Apache HTTP 服務器性能測試工具 總覽 SYNOPSIS ab  [  -A  auth-username:password  ] [ -c concurrency ...

  6. 【转】JavaScript获取节点类型、节点名称和节点值

    DOM节点信息包括节点类型(nodeType).节点名称(nodeName)和节点值(nodeValue). 节点类型 DOM节点中,每个节点都拥有不同的类型.W3C规范中常用的 DOM节点类型有以下 ...

  7. C#的提交表单方式主要有两种WebClient与HttpWebRequest

    根据黄聪:C#模拟网站页面POST数据提交表单(转) using System; using System.Collections.Generic; using System.IO; using Sy ...

  8. Kindle Unlimited上的技术书籍

            直达链接:Kindle Unlimited         前不久,亚马逊在中国也推出了电子书包月服务.消息不灵通的我过了好久才看到这个消息,随后第一时间上官网查看具体情况.      ...

  9. mysql 求最小值/最大值

    计算所有人最低工资和最高工资,需分别用到min()和max()函数.(请注意,MIN和MAX函数会忽略NULL值) select min(sal) as min_sal , max(sal) as m ...

  10. RMQ算法模板

    分别写了下标从0和1开始的两种 #include<stdio.h> #include<string.h> #include<algorithm> #include& ...