一,java中的日期格式
y | 年 |
---|---|
M | 月 |
d | 日 |
h | 时 在上午或下午 (1~12) |
H | 时 在一天中 (0~23) |
m | 分 |
s | 秒 |
S | 毫秒 |
E | 星期 |
G | 年代标志符 |
z | 时区 |
---|---|
a | 上午 / 下午 标记符 |
w | 一年中第几个星期 |
W | 一月中第几个星期 |
F | 一月中第几个星期几 |
D | 一年中的第几天 |
k | 时, 在一天中 (1~24 |
K | 时, 在上午或下午 (0~11) |
二、SimpleDateFormat类
1.构造函数
public SimpleDateFormat(String pattern);
参数pattern(格式化模板)对于SimpleDateFormat类中的parse()方法和format()方法的意义各不相同。
2.日期格式化为字符串
public final String format(Date date);
原文:
/**
* Formats a Date into a date/time string.
* @param date the time value to be formatted into a time string.
* @return the formatted time string.
*/
public final String format(Date date);
format(Date date)方法将参数date根据构造函数中的模板pattern格式转化为字符串,并作为返回值返回
即:date->string
3.字符串转化为日期
public Date parse(String source) throws ParseException
原文:
/**
* Parses text from the beginning of the given string to produce a date.
* The method may not use the entire text of the given string.
* <p>
* See the {@link #parse(String, ParsePosition)} method for more information
* on date parsing.
*
* @param source A <code>String</code> whose beginning should be parsed.
* @return A <code>Date</code> parsed from the string.
* @exception ParseException if the beginning of the specified string
* cannot be parsed.
*/
public Date parse(String source) throws ParseException;
parse(String source)方法根据构造函数中的pattern格式解析参数字符串source,将其转化为日期类型并作为返回值返回
即:string->date
注意:
1.只能解析给定字符串的开头,而且必须是pattern模板的形式,类似于正则表达式中的匹配以特定字符开头的字符串
2.如果指定字符串的开头无法分析将抛出ParseException异常。
4.案例
public class FormatDateTime {
public static void main(String[] args) {
SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm");
SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//等价于now.toLocaleString()
SimpleDateFormat myFmt3=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E ");
SimpleDateFormat myFmt4=new SimpleDateFormat(
"一年中的第 D 天 一年中第w个星期 一月中第W个星期 在一天中k时 z时区");
Date now=new Date();
System.out.println(myFmt.format(now));
System.out.println(myFmt1.format(now));
System.out.println(myFmt2.format(now));
System.out.println(myFmt3.format(now));
System.out.println(myFmt4.format(now));
System.out.println(now.toGMTString());
System.out.println(now.toLocaleString());
System.out.println(now.toString());
}
}
运行结果:
2004年12月16日 17时24分27秒
04/12/16 17:24
2004-12-16 17:24:27
2004年12月16日 17时24分27秒 星期四
一年中的第 351 天 一年中第51个星期 一月中第3个星期 在一天中17时 CST时区
16 Dec 2004 09:24:27 GMT
2004-12-16 17:24:27
Thu Dec 16 17:24:27 CST 2004
三、数据库中date类型的转换
1.MySQL中的日期格式
MySQL 使用下列数据类型在数据库中存储日期或日期/时间值:
精确到日 格式:YYYY-MM-DD
精确到秒 格式:YYYY-MM-DD HH:MM:SS
四、JDBC中的日期格式
1.两种日期类型的关系
java.sql.Data extends java.util.Date
即: java.util.Date类是java.sql.Data类型的父类
相关的继承关系
java.sql.Date extends java.util.Date
java.sql.Time extends java.util.Time
java.sql.Timestamp extends java.util.Timestamp
2.存:
常规方法先将java中的日期date转换为字符串再转换为SQL中的日期date,不建议使用强转
即将java.util.Data
包下的Data类,即常规java时间类,通过例如上述的format()
方法转换为字符串,再将此字符串通过java.sql.Data
包下的静态方法valueOf()包装成java.sql.Data
类存入数据库
方法原型:
Converts a string in JDBC date escape format to a Date value.
参数:
s – a String object representing a date in in the format "yyyy-[m]m-[d]d". The leading zero for mm and dd may also be omitted.
返回值:
a java.sql.Date object representing the given date
抛出:
IllegalArgumentException – if the date given is not in the JDBC date escape format (yyyy-[m]m-[d]d)
public static Date valueOf(String s);
3.取:
由于java.util.Date类是java.sql.Data类型的父类,所以java.sql.Data类从数据库中取出的时候,可以向上转型成java.util.Date类型。
所以可以不必通过String类间接转换。
创作不容易,对您有帮助的话可以点个赞谢谢!!