1.日期格式表达式
yyyy 年 MM 月 dd 日 hh/HH 12/24小时 mm 分 ss 秒 SS 毫秒
2.日期格式化类SimpleDateFormat(文本->日期,日期->文本)
例如:日期-》文本
/**
* 获取12小时制当前日期字符串
* @return
*/
public static String getStrDate_12(){
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date currentTime = new Date();
//java.sql.Date date=new java.sql.Date(currentTime.getTime());
String strDate = formatter.format(currentTime);
return strDate;
}
例如 文本-》日期
/**
* 将字符串类型的时间转化为Date型
* @param strDate
* @param formatDate
* @return Date
* @throws ParseException
*/
public static Date str2Date(String strDate,String formatDate) throws ParseException{
SimpleDateFormat sdf = new SimpleDateFormat(formatDate);
return sdf.parse(strDate);
}
3.日期类的继承关系
了解继承关系有助于日期类的使用。
![](https://img-my.csdn.net/uploads/201302/05/1360029552_7930.png)
util.Data->
sql.Date,sql.Time,sql.Timestamp。
尤其是经常会用到父类Date中的getTime()方法,方便各类型直接的转换。很多参数为空的构造函数都标记为过时,因此一般都用毫秒来进行日期构建。
4.数据库日期的获取处理
数据库端:datetime 类型插入 yyyy-MM-dd hh:mm:ss详细的时间值
获取日期:
rs.getDate():使用util.Date和sql.Date类型获取的yyyy-MM-dd 00:00:00,粒度只能到天,小时分钟秒都丢失。
rs.getTime():使用sql.Time类型,粒度能到hh:mm:ss,丢失年月天
rs.getTimestamp:使用sql.Timestamp类型获取,粒度完整yyyy-MM-dd hh:mm:ss,不丢失