日期和时间:
Date对象:
构造函数:Date(),Date(longmillisec)
Date方法:
方法 | 描述 |
boolean after(Date date) | 若当调用此方法的Date对象在指定日期之后返回true,否则返回false。 |
boolean before(Date date) | 若当调用此方法的Date对象在指定日期之前返回true,否则返回false。 |
Object clone( ) | 返回此对象的副本。 |
int compareTo(Date date) | 比较当调用此方法的Date对象和指定日期。两者相等时候返回0,调用对象在指定日期之前则返回负数,调用对象在指定日期之后则返回正数。 |
int compareTo(Object obj) | 若obj是Date类型则操作等同于compareTo(Date),否则它抛出ClassCastException。 |
boolean equals(Object date) | 当调用此方法的Date对象和指定日期相等时候返回true,否则返回false。 |
long getTime( ) | 返回自 1970 年 1月 1 日 00:00:00 GMT以来此 Date 对象表示的毫秒数。 |
int hashCode( ) | 返回此对象的哈希码值。 |
void setTime(long time) | 用自1970年1月1日00:00:00 GMT以后time毫秒数设置时间和日期。 |
String toString( ) | 转换Date对象为String表示形式,并返回该字符串。 |
SimpleDateFormat格式化日期:
编码:
字母 | 描述 | 示例 |
G | 纪元标记 | AD |
y | 四位年份 | 2001 |
M | 月份 | July or 07 |
d | 一个月的日期 | 10 |
h | A.M./P.M. (1~12)格式小时 | 12 |
H | 一天中的小时 (0~23) | 22 |
m | 分钟数 | 30 |
s | 秒数 | 55 |
S | 毫秒数 | 234 |
E | 星期几 | Tuesday |
D | 一年中的日子 | 360 |
F | 一个月中第几周的周几 | 2 (second Wed. in July) |
w | 一年中第几周 | 40 |
W | 一个月中第几周 | 1 |
a | A.M./P.M. 标记 | PM |
k | 一天中的小时(1~24) | 24 |
K | A.M./P.M. (0~11)格式小时 | 10 |
z | 时区 | Eastern Standard Time |
' | 文字定界符 | 类似于用于转义的字符“\” |
" | 单引号 | ` |
SimpleDateFormat格式化日期举例:
SimpleDateFormat ft = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a 'zone':zzz 'dayOfMonth':d 'dayOfYear':D"); Date now = new Date(); System.out.println(ft.format(now)); |
输出:
星期一 2017.03.13 at 06:35:13 下午 zone:CST dayOfMonth:13 dayOfYear:72 |
使用printf格式化输出日期:
转义字符:
字母 | 描述 | 示例 |
c | 完整的日期和时间 | Mon May 04 09:51:52 CDT 2009 |
F | ISO 8601 格式日期 | 2004-02-09 |
D | U.S. 格式日期 (月/日/年) | 02/09/2004 |
T | 24小时时间 | 18:05:19 |
r | 12小时时间 | 06:05:19 pm |
R | 24小时时间,不包含秒 | 18:05 |
Y | 4位年份(包含前导0) | 2004 |
y | 年份后2位(包含前导0) | 04 |
C | 年份前2位(包含前导0) | 20 |
B | 月份全称 | February |
b | 月份简称 | Feb |
m | 2位月份(包含前导0) | 02 |
d | 2位日子(包含前导0) | 03 |
e | 2位日子(不包含前导0) | 9 |
A | 星期全称 | Monday |
a | 星期简称 | Mon |
j | 3位年份(包含前导0) | 069 |
H | 2位小时(包含前导0), 00到 23 | 18 |
k | 2位小时(不包含前导0), 0到 23 | 18 |
I | 2位小时(包含前导0), 01到 12 | 06 |
l | 2位小时(不包含前导0), 1到 12 | 6 |
M | 2位分钟(包含前导0) | 05 |
S | 2位秒数(包含前导0) | 19 |
L | 3位毫秒(包含前导0) | 047 |
N | 9位纳秒(包含前导0) | 047000000 |
p | 小写上下午标志 | pm |
z | 时区偏移小时数 | -0800 |
Z | 时区 | PST |
s | 自 1970-01-01 00:00:00 GMT的秒数 | 1078884319 |
Q | 自 1970-01-01 00:00:00 GMT的毫妙 | 1078884319047 |
示例:
Date now = new Date(); System.out.printf("%tc\r\n", now); // 完整的日期和时间:Mon Mar 13 19:03:45 CST 2017 System.out.printf("%tF\r\n", now); // ISO 8601 格式日期:2017-03-13 System.out.printf("%tD\r\n", now); // U.S. 格式日期 (月/日/年):03/13/17 System.out.printf("%tT\r\n", now); // 24小时时间:19:03:45 System.out.printf("%tr\r\n", now); // 12小时时间:07:03:45 PM System.out.printf("%tR\r\n", now); // 24小时时间,不包含秒:19:03 System.out.printf("%tY\r\n", now); // 4位年份(包含前导0):2017 System.out.printf("%ty\r\n", now); // 年份后2位(包含前导0):17 System.out.printf("%tC\r\n", now); // 年份前2位(包含前导0):20 System.out.printf("%tB\r\n", now); // 月份全称:March System.out.printf("%tb\r\n", now); // 月份简称:Mar System.out.printf("%tm\r\n", now); // 2位月份(包含前导0):03 System.out.printf("%td\r\n", now); // 2位日子(包含前导0):13 System.out.printf("%te\r\n", now); // 2位日子(不包含前导0):13 System.out.printf("%tA\r\n", now); // 星期全称:Monday System.out.printf("%ta\r\n", now); // 星期简称:Mon System.out.printf("%tj\r\n", now); // 3位年份(包含前导0):072 System.out.printf("%tH\r\n", now); // 2位小时(包含前导0), [00,23]:19 System.out.printf("%tk\r\n", now); // 2位小时(不包含前导0), [0,23]:19 System.out.printf("%tI\r\n", now); // 2位小时(包含前导0), [01,12]:07 System.out.printf("%tl\r\n", now); // 2位小时(不包含前导0), [1,12]:7 System.out.printf("%tM\r\n", now); // 2位分钟(包含前导0):03 System.out.printf("%tS\r\n", now); // 2位秒数(包含前导0):45 System.out.printf("%tL\r\n", now); // 3位毫秒(包含前导0):956 System.out.printf("%tN\r\n", now); // 9位纳秒(包含前导0):956000000 System.out.printf("%tp\r\n", now); // 小写上下午标志:pm System.out.printf("%tz\r\n", now); // 时区偏移小时数:+0800 System.out.printf("%tZ\r\n", now); // 时区:CST System.out.printf("%ts\r\n", now); // 自 1970-01-01 00:00:00 GMT的秒数:1489403025 System.out.printf("%tQ\r\n", now); // 自 1970-01-01 00:00:00 GMT的毫妙:1489403025956 |
Calendar对象:
创建Calendar对象:
Calendar cal = Calendar.getInstance(); |
Calendar常量,主要用于set/add/get方法:
常量 | 描述 |
Calendar.YEAR | 年份 |
Calendar.MONTH | 月份 |
Calendar.DATE | 日期 |
Calendar.DAY_OF_MONTH | 日期,和上面的字段意义完全相同 |
Calendar.HOUR | 12小时制的小时 |
Calendar.HOUR_OF_DAY | 24小时制的小时 |
Calendar.MINUTE | 分钟 |
Calendar.SECOND | 秒 |
Calendar.DAY_OF_WEEK | 星期几 |
set/get/add方法示例:
Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, 2000); cal.set(Calendar.MONTH, 11); //0-11 cal.set(Calendar.DATE, 31); //1-31 int day = cal.get(Calendar.DAY_OF_WEEK); System.out.println(day); System.out.println(cal.getTime()); cal.add(Calendar.DATE, 1); System.out.println(cal.getTime()); |
输出:
1 Sun Dec 31 19:37:30 CST 2000 Mon Jan 01 19:37:30 CST 2001 |
GregorianCalendar类:
Calendar类实现了公历日历,GregorianCalendar是Calendar类的一个具体实现。
Calendar 的getInstance()方法返回一个默认用当前的语言环境和时区初始化的GregorianCalendar对象。GregorianCalendar定义了两个字段:AD和BC,代表公历定义的两个时代。
GregorianCalendar的构造方法:
构造方法 | 描述 |
GregorianCalendar() | 默认语言,默认时区,使用当前时间构造一个默认的 GregorianCalendar |
GregorianCalendar(int year, int month, int date) | 默认语言,默认时区,构造一个带有给定日期设置的 GregorianCalendar |
GregorianCalendar(int year, int month, int date, int hour, int minute) | 默认语言,默认时区,构造一个具有给定日期和时间设置的 GregorianCalendar |
GregorianCalendar(int year, int month, int date, int hour, int minute, int second) | 默认语言,默认时区,构造一个具有给定日期和时间设置的 GregorianCalendar |
GregorianCalendar(Locale aLocale) | 给定语言,默认时区,构造一个基于当前时间的 GregorianCalendar |
GregorianCalendar(TimeZone zone) | 默认语言,给定时区,构造一个基于当前时间的 GregorianCalendar |
GregorianCalendar(TimeZone zone, Locale aLocale) | 给定语言,给定时区构造一个基于当前时间的 GregorianCalendar |
GregorianCalendar的常用方法:
方法 | 描述 |
void add(int field, int amount) | 根据日历规则,将指定的(有符号的)时间量添加到给定的日历字段中 |
void computeFields() | 转换UTC毫秒值为时间域值 |
void computeTime() | 覆盖Calendar ,转换时间域值为UTC毫秒值 |
boolean equals(Object obj) | 比较此 GregorianCalendar 与指定的 Object |
int get(int field) | 获取指定字段的时间值 |
int getActualMaximum(int field) | 返回对于当前日期而言给定字段的最大值 |
int getActualMinimum(int field) | 返回对于当前日期而言给定字段的最小值 |
int getGreatestMinimum(int field) | 返回此 GregorianCalendar 实例给定日历字段的最大的中的最小值 |
int getLeastMaximum(int field) | 返回此 GregorianCalendar 实例给定日历字段的最小指中的最大值 |
int getMaximum(int field) | 返回此 GregorianCalendar 实例的给定日历字段的最大值。 |
int getMinimum(int field) | 返回此 GregorianCalendar 实例的给定日历字段的最小值。 |
Date getTime() | 获取日历当前时间 |
long getTimeInMillis() | 获取用长整型表示的日历的当前时间 |
TimeZone getTimeZone() | 获取时区 |
int getMinimum(int field) | 返回给定字段的最小值 |
int hashCode() | 返回hash码 |
boolean isLeapYear(int year) | 确定给定的年份是否为闰年。 |
void roll(int field, boolean up) | 在给定的时间字段上添加或减去(上/下)单个时间单元,不更改更大的字段 |
void set(int field, int value) | 用给定的值设置时间字段 |
void set(int year, int month, int date) | 设置年、月、日的值 |
void set(int year, int month, int date, int hour, int minute) | 设置年、月、日、小时、分钟的值 |
void set(int year, int month, int date, int hour, int minute, int second) | 设置年、月、日、小时、分钟、秒的值 |
void setGregorianChange(Date date) | 设置 GregorianCalendar 的更改日期 |
void setTime(Date date) | 用给定的日期设置Calendar的当前时间 |
void setTimeInMillis(long millis) | 用给定的long型毫秒数设置Calendar的当前时间 |
void setTimeZone(TimeZone value) | 用给定时区值设置当前时区 |
String toString() | 返回代表日历的字符串 |