一、Date类
构造方法
Date()分配 Date 对象并初始化此对象,以表示分配它的时间(精确到毫秒)。以本机时间为准。Date(long date)分配 Date 对象并初始化此对象,以表示自从标准基准时间(称为“历元(epoch)”,即 1970 年 1 月 1 日 00:00:00 GMT)以来的指定毫秒数。例:Date date1=new Date(10000000000L); 指历元后的10000000000毫秒所表示的时间。
方法
boolean after(Date when)测试此日期是否在指定日期之后。boolean before(Date when)测试此日期是否在指定日期之前。int compareTo(Date anotherDate)比较两个日期的顺序。 如果参数 Date 等于此 Date,则返回值 0;如果此 Date 在 Date 参数之前,则返回小于 0 的值;如果此 Date 在 Date 参数之后,则返回大于 0 的值。boolean equals(Object obj)比较两个日期是否相等。long getTime()返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。void setTime(long time)设置此 Date 对象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的时间点。String toString()把此 Date 对象转换为以下形式的 String: dow mon dd hh:mm:ss zzz yyyy 。
二、SimpleDateFormat 日期格式转换器
date输出的日期格式类型是默认类型,所以需要SimpleDateFormat 来自定义日期的格式。
默认格式:yy-MM-dd aK:m
格式:
| 字母 | 元素 |
|---|---|
| y | 年 |
| M | 月 |
| w | 年的周数 |
| W | 月的周数 |
| D | 年的天数 |
| d | 月的天数 |
| F | 月中第几周 |
| E | 周的天数 |
| a | am/pm |
| H | 一天中的小时数(0-23) |
| k | 一天中的小时数(1-24) |
| K | am/pm 中的小时数(0-11) |
| h | am/pm 中的小时数(1-12) |
| m | 分 |
| s | 秒 |
| S | 毫秒 |
| z | 时区(General time zone ) |
| Z | 时区(RFC 822 time zone ) |
例
SimpleDateFormat simple=new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
构造方法
SimpleDateFormat()用默认的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。SimpleDateFormat(String pattern)用给定的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。
方法
StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos)将给定的 Date 格式化为日期/时间字符串,并将结果添加到给定的 StringBuffer。Date parse(String text, ParsePosition pos)解析字符串的文本,生成 Date。
版权声明:本文为weixin_43934912原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。