Markdown版本笔记 | 我的GitHub首页 | 我的博客 | 我的微信 | 我的邮箱 |
---|---|---|---|---|
MyAndroidBlogs | baiqiantao | baiqiantao | bqt20094 | baiqiantao@sina.com |
目录
目录
常用案例
判断是今天还是明天
计算两个日期间相差几天
增加或减少一定的时间
判断缓存是否过期
获取一个时间,要求当月有31天
SimpleDateFormat 格式化 Date 示例
Calendar 和 Date 的妙用
Calendar 基本操作示例
TimeZone中可获取到的有用信息
Locale 中可获取到的有用信息
API
Date 日期类
Calendar 日历类
DateFormat 日期格式抽象类
SimpleDateFormat 日期格式类
TimeZone 时区类
Locale 地区类
常用案例
判断是今天还是明天
计算两个日期间相差几天
增加或减少一定的时间
判断缓存是否过期
获取一个时间,要求当月有31天
SimpleDateFormat 格式化 Date 示例
Calendar 和 Date 的妙用
Calendar 基本操作示例
TimeZone中可获取到的有用信息
Locale 中可获取到的有用信息
API
Date 日期类
Calendar 日历类
DateFormat 日期格式抽象类
SimpleDateFormat 日期格式类
TimeZone 时区类
Locale 地区类
常用案例
判断是今天还是明天
public static String getDayString(long millseconds) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
if (millseconds > calendar.getTimeInMillis()) return "明天";
else return "今天";
}
计算两个日期间相差几天
//计算两个日期间相差几天
public static long getDay(String dateStr1, String sFormat1, String dateStr2, String sFormat2) {
try {
Date date1 = new SimpleDateFormat(sFormat1).parse(dateStr1);
Date date2 = new SimpleDateFormat(sFormat2).parse(dateStr2);
Long time = Math.abs(date1.getTime() - date2.getTime());
return time / 1000 / 60 / 60 / 24;
} catch (ParseException e) {
e.printStackTrace();
}
return -1;
}
增加或减少一定的时间
//增加或减少一定的时间
public static Date addDate(Date date, int field, int amount) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(field, amount); //月 Calendar.MONTH;天 Calendar.DAY_OF_YEAR;小时 Calendar.HOUR;分 Calendar.MINUTE;秒 Calendar.SECOND
return calendar.getTime();
}
判断缓存是否过期
System.currentTimeMillis() / 1000 / 60 / 60 / 24 / 2 + "_bqt";//缓存2天
获取一个时间,要求当月有31天
public static long getAGoodTime() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2017);
calendar.set(Calendar.MONTH, 5);
calendar.set(Calendar.DAY_OF_MONTH, 16);
return calendar.getTimeInMillis();
}
SimpleDateFormat 格式化 Date 示例
public static String getDayString(long millseconds) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
if (millseconds > calendar.getTimeInMillis()) return "明天";
else return "今天";
}
//计算两个日期间相差几天
public static long getDay(String dateStr1, String sFormat1, String dateStr2, String sFormat2) {
try {
Date date1 = new SimpleDateFormat(sFormat1).parse(dateStr1);
Date date2 = new SimpleDateFormat(sFormat2).parse(dateStr2);
Long time = Math.abs(date1.getTime() - date2.getTime());
return time / 1000 / 60 / 60 / 24;
} catch (ParseException e) {
e.printStackTrace();
}
return -1;
}
//增加或减少一定的时间
public static Date addDate(Date date, int field, int amount) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(field, amount); //月 Calendar.MONTH;天 Calendar.DAY_OF_YEAR;小时 Calendar.HOUR;分 Calendar.MINUTE;秒 Calendar.SECOND
return calendar.getTime();
}
System.currentTimeMillis() / 1000 / 60 / 60 / 24 / 2 + "_bqt";//缓存2天
public static long getAGoodTime() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2017);
calendar.set(Calendar.MONTH, 5);
calendar.set(Calendar.DAY_OF_MONTH, 16);
return calendar.getTimeInMillis();
}
默认的格式
System.out.println(SimpleDateFormat.getInstance().format(new Date()));//16-5-1 下午7:47
System.out.println(SimpleDateFormat.getDateInstance().format(new Date()));//2016-5-1
System.out.println(SimpleDateFormat.getTimeInstance().format(new Date()));//19:46:04
System.out.println(SimpleDateFormat.getDateTimeInstance().format(new Date()));//2016-5-1 19:45:10
不同风格格式的Date
System.out.println(SimpleDateFormat.getDateInstance(DateFormat.DEFAULT).format(date));//2016-9-1
System.out.println(SimpleDateFormat.getDateInstance(DateFormat.SHORT).format(date));//16-9-1
System.out.println(SimpleDateFormat.getDateInstance(DateFormat.MEDIUM).format(date));//2016-9-1
System.out.println(SimpleDateFormat.getDateInstance(DateFormat.LONG).format(date));//2016年9月1日
System.out.println(SimpleDateFormat.getDateInstance(DateFormat.FULL).format(date) + "\n");//2016年9月1日 星期四
不同风格格式的Time
System.out.println(SimpleDateFormat.getTimeInstance(DateFormat.DEFAULT).format(date));//18:04:37
System.out.println(SimpleDateFormat.getTimeInstance(DateFormat.SHORT).format(date));//下午6:04
System.out.println(SimpleDateFormat.getTimeInstance(DateFormat.MEDIUM).format(date));//18:04:37
System.out.println(SimpleDateFormat.getTimeInstance(DateFormat.LONG).format(date));//下午06时04分37秒
System.out.println(SimpleDateFormat.getTimeInstance(DateFormat.FULL).format(date));//下午06时04分37秒 CST
自定义格式
System.out.println(new SimpleDateFormat("yyyy.MM.dd a HH:mm:ss SSS", Locale.getDefault()).format(new Date()));//2016.05.01 下午 22:29:46 583
System.out.println(new SimpleDateFormat("M月d日 m分 第D天 H点(ah点) s秒S微秒").format(new Date()));//5月1日 57分 第122天 22点(下午10点) 33秒95微秒
System.out.println(new SimpleDateFormat("z(zzzz) Gyy年第w周 M月第W周 E").format(new Date()));//CST(中国标准时间) 公元16年第19周 5月第1周 星期日
Calendar 和 Date 的妙用
public static void main(String[] args) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse("2016-05-1 19:45:8");
System.out.println(format.format(date));//2016-05-01 19:45:08
System.out.println(format.format(reFormatDate(date)));//2017-10-16 19:45:08
}
public static Date reFormatDate(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);// date中仅时和分时有效的,年月日重新格式化
calendar.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR));
calendar.set(Calendar.MONTH, Calendar.getInstance().get(Calendar.MONTH));
calendar.set(Calendar.DAY_OF_MONTH, Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
return calendar.getTime();
}
Calendar 基本操作示例
Calendar calendar = Calendar.getInstance();
//年月日
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;//这里一定要注意:0代表1月,11表示十二月,12表示下一年的1月…
int day = calendar.get(Calendar.DAY_OF_MONTH);
//上下午
String apStr = calendar.get(Calendar.AM_PM) == 1 ? "下午" : "上午";//0代表上午,1代表下午
//时分秒
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);//由 0 至 61 的整数表示,值 60 和 61 只对闰秒发生
//星期
String[] weeks = { "日", "一", "二", "三", "四", "五", "六" };
int weekNum = calendar.get(Calendar.DAY_OF_WEEK);//1表示星期日,7表示星期六
String weekStr = "星期" + weeks[weekNum - 1];
String string = year + "年" + month + "月" + day + "日" + " " + weekStr + " " + apStr + hour + ":" + minute + ":" + second;
System.out.println(string);//2016年5月1日 星期日 下午23:27:26
TimeZone中可获取到的有用信息
TimeZone timeZone = TimeZone.getDefault();//获取此主机的默认 TimeZone
System.out.println(timeZone.getDisplayName());//中国标准时间
System.out.println(timeZone.getDisplayName(true, TimeZone.SHORT));//CDT。如果为 true,则返回夏令时名称
System.out.println(timeZone.getDisplayName(false, TimeZone.SHORT));//CST
System.out.println(timeZone.getDisplayName(true, TimeZone.LONG));//中国夏令时
System.out.println(timeZone.getDisplayName(false, TimeZone.LONG));//中国标准时间
System.out.println(timeZone.getID());//Asia/Shanghai
System.out.println(timeZone.getRawOffset() / 1000 / 60 / 60);//8。也就是8小时,因为我们是在东八区
System.out.println(Arrays.toString(TimeZone.getAvailableIDs()));//获取受支持的所有可用ID
Locale 中可获取到的有用信息
Locale locale = Locale.getDefault();//获得此 Java 虚拟机实例的当前默认语言环境值
System.out.println(locale.getCountry());//CN。国家
System.out.println(locale.getDisplayCountry());//中国
System.out.println(locale.getLanguage());//zh。语言
System.out.println(locale.getDisplayLanguage());//中文
System.out.println(Locale.CHINESE);//zh。代表中文环境
System.out.println(Locale.CHINA);//zh_CN。代表*的中文环境
System.out.println(Locale.*);//zh_TW。代表中国*的中文环境
System.out.println(Locale.ENGLISH);//en。代表英文环境
System.out.println(Locale.US);//en_US。代表美国的英文环境
System.out.println(Locale.UK);//en_GB。代表英国的英文环境
System.out.println(Arrays.toString(Locale.getAvailableLocales()));//返回所有已安装语言环境的数组
所有已安装语言环境
[, ar_AE, ar_JO, ar_SY, hr_HR, fr_BE, es_PA, mt_MT, es_VE, bg, zh_TW, it, ko, uk, lv, da_DK, es_PR, vi_VN, en_US, sr_ME, sv_SE, es_BO, en_SG, ar_BH, pt, ar_SA, sk, ar_YE, hi_IN, ga, en_MT, fi_FI, et, sv, cs, sr_BA_#Latn, el, uk_UA, hu, fr_CH, in, es_AR, ar_EG, ja_JP_JP_#u-ca-japanese, es_SV, pt_BR, be, is_IS, cs_CZ, es, pl_PL, tr, ca_ES, sr_CS, ms_MY, hr, lt, es_ES, es_CO, bg_BG, sq, fr, ja, sr_BA, is, es_PY, de, es_EC, es_US, ar_SD, en, ro_RO, en_PH, ca, ar_TN, sr_ME_#Latn, es_GT, sl, ko_KR, el_CY, es_MX, ru_RU, es_HN, zh_HK, no_NO_NY, hu_HU, th_TH, ar_IQ, es_CL, fi, ar_MA, ga_IE, mk, tr_TR, et_EE, ar_QA, sr__#Latn, pt_PT, fr_LU, ar_OM, th, sq_AL, es_DO, es_CU, ar, ru, en_NZ, sr_RS, de_CH, es_UY, ms, el_GR, iw_IL, en_ZA, th_TH_TH_#u-nu-thai, hi, fr_FR, de_AT, nl, no_NO, en_AU, vi, nl_NL, fr_CA, lv_LV, de_LU, es_CR, ar_KW, sr, ar_LY, mt, it_CH, da, de_DE, ar_DZ, sk_SK, lt_LT, it_IT, en_IE, zh_SG, ro, en_CA, nl_BE, no, pl, zh_CN, ja_JP, de_GR, sr_RS_#Latn, iw, en_IN, ar_LB, es_NI, zh, mk_MK, be_BY, sl_SI, es_PE, in_ID, en_GB]
API
Date 日期类
构造方法
- Date() 将当前日期和时间封装成Date对象(精确到毫秒)。
- Date(long date) 将指定毫秒值封装成Date对象,以表示自从标准基准时间(即 1970 年 1 月 1 日00:00:00 GMT)以来的指定毫秒数。
废弃的
- Date(int year, int month, int date) 已过时。 从 JDK 1.1 开始,由 Calendar.set(year + 1900, month, date) 或GregorianCalendar(year + 1900, month, date) 取代
- Date(int year, int month, int date, int hrs, int min) 已过时。 从 JDK 1.1 开始,由 Calendar.set(year + 1900, month, date, hrs, min) 或 GregorianCalendar(year + 1900, month, date, hrs, min) 取代
- Date(int year, int month, int date, int hrs, int min, int sec) 已过时。 从 JDK 1.1 开始,由 Calendar.set(year + 1900, month, date, hrs, min, sec) 或 GregorianCalendar(year + 1900, month, date, hrs, min, sec) 取代
- Date(String s) 已过时。 从 JDK 1.1 开始,由 DateFormat.parse(String s) 取代
基本方法
- boolean after(Date when) 测试此日期是否在指定日期之后。
- boolean before(Date when) 测试此日期是否在指定日期之前。
- long getTime() 返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
- void setTime(long time) 设置此 Date 对象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的时间点。
重写Object中的方法
- Object clone() 返回此对象的副本。
- int compareTo(Date anotherDate) 比较两个日期的顺序。
- boolean equals(Object obj) 比较两个日期的相等性。
- int hashCode() 返回此对象的哈希码值。
- String toString() 把此 Date 对象转换为以下形式的 String: dow mon dd hh:mm:ss zzz yyyy ,其中: dow 是一周中的某一天。比如【2016-05-01 19:45:08】为【Sun May 01 19:45:08 CST 2016】
废弃的方法
- int getDate() 已过时。 从 JDK 1.1 开始,由 Calendar.get(Calendar.DAY_OF_MONTH) 取代。
- int getDay() 已过时。 从 JDK 1.1 开始,由 Calendar.get(Calendar.DAY_OF_WEEK) 取代。
- int getHours() 已过时。 从 JDK 1.1 开始,由 Calendar.get(Calendar.HOUR_OF_DAY) 取代。
- int getMinutes() 已过时。 从 JDK 1.1 开始,由 Calendar.get(Calendar.MINUTE) 取代。
- int getMonth() 已过时。 从 JDK 1.1 开始,由 Calendar.get(Calendar.MONTH) 取代。
- int getSeconds() 已过时。 从 JDK 1.1 开始,由 Calendar.get(Calendar.SECOND) 取代。
- int getTimezoneOffset() 已过时。 从 JDK 1.1 开始,由 -(Calendar.get(Calendar.ZONE_OFFSET) + Calendar.get(Calendar.DST_OFFSET)) / (60 * 1000) 取代。
- int getYear() 已过时。 从 JDK 1.1 开始,由 Calendar.get(Calendar.YEAR) - 1900 取代。
- static long parse(String s) 已过时。 从 JDK 1.1 开始,由 DateFormat.parse(String s) 取代。
- void setDate(int date) 已过时。 从 JDK 1.1 开始,由 Calendar.set(Calendar.DAY_OF_MONTH, int date) 取代。
- void setHours(int hours) 已过时。 从 JDK 1.1 开始,由 Calendar.set(Calendar.HOUR_OF_DAY, int hours) 取代。
- void setMinutes(int minutes) 已过时。 从 JDK 1.1 开始,由 Calendar.set(Calendar.MINUTE, int minutes) 取代。
- void setMonth(int month) 已过时。 从 JDK 1.1 开始,由 Calendar.set(Calendar.MONTH, int month) 取代。
- void setSeconds(int seconds) 已过时。 从 JDK 1.1 开始,由 Calendar.set(Calendar.SECOND, int seconds) 取代。
- void setYear(int year) 已过时。 从 JDK 1.1 开始,由 Calendar.set(Calendar.YEAR, year + 1900) 取代。
- String toGMTString() 已过时。 从 JDK 1.1 开始,由 DateFormat.format(Date date) 取代,使用 GMT TimeZone。
- String toLocaleString() 已过时。 从 JDK 1.1 开始,由 DateFormat.format(Date date) 取代。
- static long UTC(int year, int month, int date, int hrs, int min, int sec) 已过时。 从 JDK 1.1 开始,由Calendar.set(year + 1900, month, date, hrs, min, sec) 或 GregorianCalendar(year + 1900, month, date, hrs, min, sec) 取代,使用 UTC TimeZone,后跟 Calendar.getTime().getTime()。
Calendar 日历类
成员变量&常量
没有指定类型的全部为static int 类型
- ALL_STYLES 指示所有风格名称的 getDisplayNames 的风格说明符,比如 "January" 和 "Jan"。
- AM 指示从午夜到中午之前这段时间的 AM_PM 字段值。
- AM_PM get 和 set 的字段数字,指示 HOUR 是在中午之前还是在中午之后。
- APRIL 指示在格里高利历和罗马儒略历中一年中第四个月的 MONTH 字段值。
- boolean areFieldsSet 如果 fields[] 与当前的设置时间同步,则返回 true。
- AUGUST 指示在格里高利历和罗马儒略历中一年中第八个月的 MONTH 字段值。
- DATE get 和 set 的字段数字,指示一个月中的某天。
- DAY_OF_MONTH get 和 set 的字段数字,指示一个月中的某天。
- DAY_OF_WEEK get 和 set 的字段数字,指示一个星期中的某天。
- DAY_OF_WEEK_IN_MONTH get 和 set 的字段数字,指示当前月中的第几个星期。
- DAY_OF_YEAR get 和 set 的字段数字,指示当前年中的天数。
- DECEMBER 指示在格里高利历和罗马儒略历中一年中第十二个月的 MONTH 字段值。
- DST_OFFSET get 和 set 的字段数字,以毫秒为单位指示夏令时的偏移量。
- ERA 指示年代的 get 和 set 的字段数字,比如罗马儒略历中的 AD 或 BC。
- FEBRUARY 指示在格里高利历和罗马儒略历中一年中第二个月的 MONTH 字段值。
- FIELD_COUNT get 和 set 可识别的不同字段的数量。
- protected int[] fields 此日历当前设置时间的日历字段值。
- FRIDAY 指示 Friday 的 DAY_OF_WEEK 字段值。
- HOUR get 和 set 的字段数字,指示上午或下午的小时。
- HOUR_OF_DAY get 和 set 的字段数字,指示一天中的小时。
- boolean[] isSet 通知是否设置了该日历某一指定日历字段的标志。
- boolean isTimeSet 如果 time 值是一个有效值,则返回 true。
- JANUARY 指示在格里高利历和罗马儒略历中一年中第一个月的 MONTH 字段值。
- JULY 指示在格里高利历和罗马儒略历中一年中第七个月的 MONTH 字段值。
- JUNE 指示在格里高利历和罗马儒略历中一年中第六个月的 MONTH 字段值。
- LONG 指示长名称的 getDisplayName 和 getDisplayNames 的风格说明符,比如 "January"。
- MARCH 指示在格里高利历和罗马儒略历中一年中第三个月的 MONTH 字段值。
- MAY 指示在格里高利历和罗马儒略历中一年中第五个月的 MONTH 字段值。
- MILLISECOND get 和 set 的字段数字,指示一秒中的毫秒。
- MINUTE get 和 set 的字段数字,指示一小时中的分钟。
- MONDAY 指示 Monday 的 DAY_OF_WEEK 字段值。
- MONTH 指示月份的 get 和 set 的字段数字。
- NOVEMBER 指示在格里高利历和罗马儒略历中一年中第十一个月的 MONTH 字段值。
- OCTOBER 指示在格里高利历和罗马儒略历中一年中第十个月的 MONTH 字段值。
- PM 指示从中午到午夜之前这段时间的 AM_PM 字段值。
- SATURDAY 指示 Saturday 的 DAY_OF_WEEK 字段值。
- SECOND get 和 set 的字段数字,指示一分钟中的秒。
- SEPTEMBER 指示在格里高利历和罗马儒略历中一年中第九个月的 MONTH 字段值。
- SHORT 指示短名称的 getDisplayName 和 getDisplayNames 的风格说明符,比如 "Jan"。
- SUNDAY 指示 Sunday 的 DAY_OF_WEEK 字段值。
- THURSDAY 指示 Thursday 的 DAY_OF_WEEK 字段值。
- TUESDAY 指示 Tuesday 的 DAY_OF_WEEK 字段值。
- UNDECIMBER 指示一年中第十三个月的 MONTH 字段值。
- WEDNESDAY 指示 Wednesday 的 DAY_OF_WEEK 字段值。
- WEEK_OF_MONTH get 和 set 的字段数字,指示当前月中的星期数。
- WEEK_OF_YEAR get 和 set 的字段数字,指示当前年中的星期数。
- YEAR 指示年的 get 和 set 的字段数字。
- ZONE_OFFSET get 和 set 的字段数字,以毫秒为单位指示距 GMT 的大致偏移量。
静态方法
- static Locale[] getAvailableLocales() 返回所有语言环境的数组,此类的 getInstance 方法可以为其返回本地化的实例。
- static Calendar getInstance() 使用默认时区和语言环境获得一个日历。
- static Calendar getInstance(Locale aLocale) 使用默认时区和指定语言环境获得一个日历。
- static Calendar getInstance(TimeZone zone) 使用指定时区和默认语言环境获得一个日历。
- static Calendar getInstance(TimeZone zone, Locale aLocale) 使用指定时区和语言环境获得一个日历。
get操作
- int get(int field) 返回给定日历字段的值。
- Date getTime() 返回一个表示此 Calendar 时间值(从历元至现在的毫秒偏移量)的 Date 对象。
- long getTimeInMillis() 返回此 Calendar 的时间值,以毫秒为单位。
- int getFirstDayOfWeek() 获取一星期的第一天;例如,在美国这一天是 SUNDAY,而在法国这一天是MONDAY
- TimeZone getTimeZone() 获得时区。
- String getDisplayName(int field, int style, Locale locale) 返回给定 style 和 locale 下的日历field 值的字符串表示形式。
- Map getDisplayNames(int field, int style, Locale locale) 返回给定 style 和 locale 下包含日历field 所有名称的 Map 及其相应字段值。
set操作
- void set(int field, int value) 将给定的日历字段设置为给定值。
- void set(int year, int month, int date) 设置日历字段 YEAR、MONTH 和 DAY_OF_MONTH 的值。
- void set(int year, int month, int date, int hourOfDay, int minute) 设置日历字段YEAR、MONTH、DAY_OF_MONTH、HOUR_OF_DAY 和 MINUTE 的值。
- void set(int year, int month, int date, int hourOfDay, int minute, int second) 设置字段YEAR、MONTH 等值
- void setTime(Date date) 使用给定的 Date 设置此 Calendar 的时间。
- void setTimeInMillis(long millis) 用给定的 long 值设置此 Calendar 的当前时间值。
- void setFirstDayOfWeek(int value) 设置一星期的第一天是哪一天;例如,在美国,这一天是 SUNDAY,而在法国,这一天是 MONDAY。
- void setTimeZone(TimeZone value) 使用给定的时区值来设置时区。
Min、Max操作
- int getActualMaximum(int field) 给定此 Calendar 的时间值,返回指定日历字段可能拥有的最大值。
- int getActualMinimum(int field) 给定此 Calendar 的时间值,返回指定日历字段可能拥有的最小值。
- int getGreatestMinimum(int field) 返回此 Calendar 实例给定日历字段的最高的最小值。
- int getLeastMaximum(int field) 返回此 Calendar 实例给定日历字段的最低的最大值。
- int getMaximum(int field) 返回此 Calendar 实例给定日历字段的最大值。
- int getMinimalDaysInFirstWeek() 获取一年中第一个星期所需的最少天数,例如,如果定义第一个星期包含一年第一个月的第一天,则此方法将返回 1。
- int getMinimum(int field) 返回此 Calendar 实例给定日历字段的最小值。
void setMinimalDaysInFirstWeek(int value) 设置一年中第一个星期所需的最少天数
boolean相关操作
- boolean after(Object when) 判断此 Calendar 表示的时间是否在指定 Object 表示的时间之后,返回判断结果。
- boolean before(Object when) 判断此 Calendar 表示的时间是否在指定 Object 表示的时间之前,返回判断结果。
- boolean isSet(int field) 确定给定日历字段是否已经设置了一个值,其中包括因为调用 get 方法触发内部字段计算而导致已经设置该值的情况。
- boolean isLenient() 判断日期/时间的解释是否为宽松的。
- void setLenient(boolean lenient) 指定日期/时间解释是否是宽松的。
其他时间值操作
- void add(int field, int amount) 根据日历的规则,为给定的日历字段添加或减去指定的时间量。
- void clear() 将此 Calendar 的所日历字段值和时间值(从历元至现在的毫秒偏移量)设置成未定义。
- void clear(int field) 将此 Calendar 的给定日历字段值和时间值(从历元至现在的毫秒偏移量)设置成未定义。
- void roll(int field, boolean up) 在给定的时间字段上添加或减去(上/下)单个时间单元,不更改更大的字段。
- void roll(int field, int amount) 向指定日历字段添加指定(有符号的)时间量,不更改更大的字段。
重写Object中的方法
- Object clone() 创建并返回此对象的一个副本。
- int compareTo(Calendar anotherCalendar) 比较两个 Calendar 对象表示的时间值(从历元至现在的毫秒偏移量)
- boolean equals(Object obj) 将此 Calendar 与指定 Object 比较。
- int hashCode() 返回该此日历的哈希码。
- String toString() 返回此日历的字符串表示形式。
DateFormat 日期格式抽象类
成员变量&常量
- AM_PM_FIELD 用于对齐 AM_PM 字段的有用常量。
- protected Calendar calendar DateFormat使用calendar来生成实现日期和时间格式化所需的时间字段值
- DATE_FIELD 用于对齐 DATE 字段的有用常量。
- DAY_OF_WEEK_FIELD 用于对齐 DAY_OF_WEEK 字段的有用常量。
- DAY_OF_WEEK_IN_MONTH_FIELD 用于对齐 DAY_OF_WEEK_IN_MONTH 字段的有用常量
- DAY_OF_YEAR_FIELD 用于对齐 DAY_OF_YEAR 字段的有用常量。
- DEFAULT 用于默认模式的常量。
- ERA_FIELD 用于对齐 ERA 字段的有用常量。
- FULL 用于 FULL 模式的常量。
- HOUR_OF_DAY0_FIELD 用于对齐从 0 开始的 HOUR_OF_DAY 字段的有用常量。
- HOUR_OF_DAY1_FIELD 用于对齐从 1 开始的 HOUR_OF_DAY 字段的有用常量。
- HOUR0_FIELD 用于对齐从 0 开始的 HOUR 字段的有用常量。
- HOUR1_FIELD 用于对齐从 1 开始的 HOUR 字段的有用常量。
- LONG 用于 LONG 模式的常量。
- MEDIUM 用于 MEDIUM 模式的常量。
- MILLISECOND_FIELD 用于对齐 MILLISECOND 字段的有用常量。
- MINUTE_FIELD 用于对齐 MINUTE 字段的有用常量。
- MONTH_FIELD 用于对齐 MONTH 字段的有用常量。
- protected NumberFormat numberFormat 数字格式器,DateFormat 用其来格式化日期和时间中的数字。
- SECOND_FIELD 用于对齐 SECOND 字段的有用常量。
- SHORT 用于 SHORT 模式的常量。
- TIMEZONE_FIELD 用于对齐 TIMEZONE 字段的有用常量。
- WEEK_OF_MONTH_FIELD 用于对齐 WEEK_OF_MONTH 字段的有用常量。
- WEEK_OF_YEAR_FIELD 用于对齐 WEEK_OF_YEAR 字段的有用常量。
- YEAR_FIELD 用于对齐 YEAR 字段的有用常量。
静态方法
- static Locale[] getAvailableLocales() 返回所有语言环境的数组,此类的
get**Instance
方法可以为其返回已本地化的实例。 - static DateFormat getInstance() 获取为日期和时间使用 SHORT 风格的默认日期/时间格式器。
- static DateFormat getTimeInstance() 获取时间格式器,该格式器具有默认语言环境的默认格式化风格
- static DateFormat getTimeInstance(int style) 获取具有默认语言环境的给定格式化风格的时间格式器
- static DateFormat getTimeInstance(int style, Locale aLocale) 给定语言环境的给定格式化风格
- static DateFormat getDateInstance() 获取日期格式器,该格式器具有默认语言环境的默认格式化风格
- static DateFormat getDateInstance(int style) 获取日期格式器,该格式器具有默认语言环境的给定格式化风格
- static DateFormat getDateInstance(int style, Locale aLocale) 给定语言环境的给定格式化风格
- static DateFormat getDateTimeInstance() 默认语言环境的默认格式化风格的日期/时间格式器
- static DateFormat getDateTimeInstance(int dateStyle, int timeStyle) 默认语言环境的给定日期和时间格式化风格。
- static DateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale) 给定语言环境的给定格式化风格
format方法
- String format(Date date) 将一个 Date 格式化为日期/时间字符串。
- abstract StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) 将一个 Date 格式化为日期/时间字符串。
- StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition fieldPosition) 重写Format
parse方法
- Date parse(String source) 从给定字符串的开始解析文本,以生成一个日期对象。
- abstract Date parse(String source, ParsePosition pos) 根据给定的解析位置开始解析日期/时间字符串
- Object parseObject(String source, ParsePosition pos) 解析字符串中的文本,以生成一个 Date
get方法
- Calendar getCalendar() 获取与此日期/时间格式器关联的日历。
- NumberFormat getNumberFormat() 获取此日期/时间格式器用于格式化和解析时间的数字格式器
- TimeZone getTimeZone() 获取时区。
- boolean isLenient() 判断日期/时间解析是否为不严格的。
set方法
- void setCalendar(Calendar newCalendar) 设置此日期格式所使用的日历。
- void setNumberFormat(NumberFormat newNumberFormat) 允许用户设置数字格式器。
- void setTimeZone(TimeZone zone) 为此 DateFormat 对象的日历设置时区。
- void setLenient(boolean lenient) 指定日期/时间解析是否不严格。
重写Object中的方法
- Object clone() 重写 Cloneable
- boolean equals(Object obj) 重写 equals
- int hashCode() 重写 hashCode
SimpleDateFormat 日期格式类
日期和时间模式字符串规则:
- 未加引号的全部字母 ('A' 到 'Z' 和 'a' 到 'z' )被解释为模式字母,用来表示日期或时间字符串元素
- 字母需使用单引号 ' 引起来,以免进行解释,如【'bqt'】
- 所有其他字符均不解释,只是在格式化时将它们简单复制到输出字符串,或者在解析时与输入字符串进行匹配
- 模式字母通常是重复的,不同个数的解析结果可能是相同的,也可能是不同的,建议根据显示时的数量确定字母的个数
- 大小写一般情况下表示的是同一概念下的不同表示,如h表示12小时制中的时间,H表示24小时制中的时间;
- 但也不完全是这样,比如M表示月份,m表示分钟
构造方法
- SimpleDateFormat() 用默认的模式和默认语言环境的日期格式符号构造 SimpleDateFormat
- SimpleDateFormat(String pattern) 用给定的模式和默认语言环境的日期格式符号构造 SimpleDateFormat
- SimpleDateFormat(String pattern, DateFormatSymbols formatSymbols) 用给定的模式和日期符号构造…
- SimpleDateFormat(String pattern, Locale locale) 用给定的【模式】和给定语言【环境】的默认日期格式符号构造
静态方法
getInstance()/getDateInstance()/getTimeInstance()/getDateTimeInstance() 获取日期/时间格式器。这些都是从父类继承过来的方法。
format方法
- String format(Date date) 将一个 Date 格式化为日期/时间字符串。这是从父类继承过来的方法。
- StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos) 将给定的 Date 格式化为日期/时间字符串,并将结果添加到给定的StringBuffer
- AttributedCharacterIterator formatToCharacterIterator(Object obj) 格式化生成对象。
parse方法
- Date parse(String source) 从给定字符串的开始解析文本,以生成一个日期对象。这是从父类继承过来的方法。
- Date parse(String text, ParsePosition pos) 解析字符串的文本,生成 Date。
Pattern相关的方法
- void applyLocalizedPattern(String pattern) 将给定的本地化模式字符串应用于此日期格式。
- void applyPattern(String pattern) 将给定模式字符串应用于此日期格式。
- String toLocalizedPattern() 返回描述此日期格式的本地化模式字符串。
- String toPattern() 返回描述此日期格式的模式字符串。
其他方法
- Date get2DigitYearStart()返回在 100 年周期内被解释的两位数字年份的开始日期
- void set2DigitYearStart(Date startDate) 设置 100 年周期的两位数年份,该年份将被解释为从用户指定的日期开始。
- DateFormatSymbols getDateFormatSymbols() 取此日期格式的日期和时间格式符号的一个副本
- void setDateFormatSymbols(DateFormatSymbols newFormatSymbols) 设置此日期格式的日期和时间格式符号。
重写Object中的方法
- Object clone() 创建此 SimpleDateFormat 的一个副本。
- boolean equals(Object obj) 比较给定对象与此 SimpleDateFormat 的相等性。
- int hashCode() 返回此 SimpleDateFormat 对象的哈希码值。
TimeZone 时区类
构造方法
TimeZone() 单独的构造方法。
成员变量&常量
- static int LONG 指出长名称的 getDisplayName() 的风格说明符。
- static int SHORT 指出短名称(比如 "PST")的 getDisplayName() 的风格说明符。
静态方法
- static String[] getAvailableIDs() 获取受支持的所有可用 ID。
- static String[] getAvailableIDs(int rawOffset) 根据给定的时区偏移量(以毫秒为单位)获取可用的 ID。
- static TimeZone getDefault() 获取此主机的默认 TimeZone。
- static TimeZone getTimeZone(String ID) 获取给定 ID 的 TimeZone。
- static TimeZone getTimeZone(ZoneId zoneId) @since 1.8
- static void setDefault(TimeZone zone) 设置由 getDefault 方法返回的 TimeZone。
展示名称相关方法
- String getDisplayName() 返回适合于展示给默认区域的用户的时区名称。比如【中国标准时间】
- String getDisplayName(boolean daylight, int style) 返回适合于展示给默认区域的用户的时区名称。
- String getDisplayName(boolean daylight, int style, Locale locale) 返回适合于展示给指定区域的用户的时区名称。
- String getDisplayName(Locale locale) 返回适合于展示给指定区域的用户的时区名称。
Offset相关方法
- int getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds) 获取当前日期的时区偏移量(在夏令时情况下进行修改)。
- int getOffset(long date) 从给定日期的 UTC 返回此时区的偏移量。
- int getRawOffset() 返回添加到 UTC 以获取此时区中的标准时间的时间量(以毫秒为单位)。比如【TimeZone.getDefault().getRawOffset() / 1000 / 60 / 60)】的结果为 8 (小时)
- void setRawOffset(int offsetMillis) 把基准时区偏移量设置到 GMT。
其他方法
- String getID() 获取此时区的 ID。比如【Asia/Shanghai】
- void setID(String ID) 设置时区 ID。
- int getDSTSavings() 返回要添加到本地标准时间以获取本地挂钟时间的时间量。
- boolean hasSameRules(TimeZone other) 如果此区域和另一个区域具有相同的规则和偏移量,也就是说,如果此区域只可能在 ID 上与另一个区域不同,则返回 true。
- boolean inDaylightTime(Date date) 查询给定的日期是否在此时区的夏令时中。
- boolean useDaylightTime() 查询此时区是否使用夏令时。
- Object clone() 创建此 TimeZone 的一个副本。
Locale 地区类
时区ID写法【Etc/GMT+0】或【Etc/GMT-0】
构造方法
- Locale(String language) 根据语言代码构造一个语言环境。
- Locale(String language, String country) 根据语言和国家/地区构造一个语言环境。
- Locale(String language, String country, String variant) 根据语言、国家/地区和变量构造一个语言环境。
成员变量&常量
- static Locale CANADA 用于表示国家/地区的有用常量。
- static Locale CANADA_FRENCH 用于表示国家/地区的有用常量。
- static Locale CHINA 用于表示国家/地区的有用常量。
- static Locale CHINESE 用于表示语言的有用常量。
- static Locale ENGLISH 用于表示语言的有用常量。
- static Locale FRANCE 用于表示国家/地区的有用常量。
- static Locale FRENCH 用于表示语言的有用常量。
- static Locale GERMAN 用于表示语言的有用常量。
- static Locale GERMANY 用于表示国家/地区的有用常量。
- static Locale ITALIAN 用于表示语言的有用常量。
- static Locale ITALY 用于表示国家/地区的有用常量。
- static Locale JAPAN 用于表示国家/地区的有用常量。
- static Locale JAPANESE 用于表示语言的有用常量。
- static Locale KOREA 用于表示国家/地区的有用常量。
- static Locale KOREAN 用于表示语言的有用常量。
- static Locale PRC 用于表示国家/地区的有用常量。
- static Locale ROOT 用于表示根语言环境的有用常量。
- static Locale SIMPLIFIED_CHINESE 用于表示语言的有用常量。
- static Locale * 用于表示国家/地区的有用常量。
- static Locale TRADITIONAL_CHINESE 用于表示语言的有用常量。
- static Locale UK 用于表示国家/地区的有用常量。
- static Locale US 用于表示国家/地区的有用常量。
静态方法
- static Locale[] getAvailableLocales() 返回所有已安装语言环境的数组。
- static Locale getDefault() 获得此 Java 虚拟机实例的当前默认语言环境值。
- static String[] getISOCountries() 返回 ISO 3166 中所定义的所有两字母国家/地区代码。
- static String[] getISOLanguages() 返回 ISO 639 中所定义的所有两字母语言代码。
- static void setDefault(Locale newLocale) 为此 Java 虚拟机实例设置默认语言环境。
get方法
- String getCountry() 返回此语言环境的国家/地区代码,将为空字符串或大写的 ISO 3166 两字母代码。 比如【CN】
- String getLanguage() 返回此语言环境的语言代码,可以是空字符串或小写的 ISO 639 代码。 比如【zh】
- String getISO3Country() 返回此语言环境国家/地区的三字母缩写。 比如【CHN】
- String getISO3Language() 返回此语言环境语言的三字母缩写。 比如【zho】
- String getVariant() 返回此语言环境的变量代码。If the locale doesn't specify a variant code, this function returns the empty string.
getDisplay方法
- String getDisplayCountry() 返回适合向用户显示的语言环境国家/地区名。比如【中国】
- String getDisplayCountry(Locale inLocale) 返回适合向用户显示的语言环境国家/地区名。
- String getDisplayLanguage() 返回适合向用户显示的语言环境语言名。 比如【中文】
- String getDisplayLanguage(Locale inLocale) 返回适合向用户显示的语言环境语言名。
- String getDisplayName() 返回适合向用户显示的语言环境名。 比如【中文 (中国)】
- String getDisplayName(Locale inLocale) 返回适合向用户显示的语言环境名。
- String getDisplayVariant() 返回适合向用户显示的语言环境变量代码名。
- String getDisplayVariant(Locale inLocale) 返回适合向用户显示的语言环境变量代码名。
重写Object中的方法
- Object clone() 重写 Cloneable。
- boolean equals(Object obj) 如果该 Locale 等于另一个对象,则返回 true。
- int hashCode() 重写 hashCode。
- String toString() 使用由下划线分隔的语言、国家/地区和变量来获取整个语言环境的编程名称。 比如【zh_CN】
2017-8-25