常用类库 Date DateFormat Calendar
常用类库之时间类库
java.util.Date
以下均为未过时方法(官方)
构造函数
-
Date()
分配Date对象并对其进行初始化,使其表示分配时间,测量Date到毫秒。 -
Date(long date)
分配Date对象并初始化它以表示自标准基准时间(称为“纪元”)以来的指定毫秒数,即1970年1月1日00:00:00 GMT。
Date d1 = new Date();
//Sun Jul 26 00:01:03 CST 2020
Date d2 = new Date(1000L);
//Thu Jan 01 08:00:01 CST 1970
//请深入理解斜体字段意思。
成员方法
-
boolean after(Date when)
测试此日期是否在指定日期之后。 -
boolean before(Date when)
测试此日期是否在指定日期之前。 -
Object clone()
返回此对象的副本。 -
boolean equals(Object obj)
比较两个相等的日期。 -
static Date from(Instant instant)
从Instant对象获得Date的实例。
boolean after = d1.after(d2);
//指定d1是否在d2之后,并返回boolear值。
Object clone = d1.clone();
//克隆对象并返回Object对象。
boolean equals = d.equals(d2);
/**当且仅当参数不是null并且是Date对象时,结果为true ,该对象表示与此对象相同的时间点,以毫秒为单位。
*因此,当且仅当getTime方法为两者返回相同的long值时,两个Date对象相等。*/
Date from = Date.from(Calendar.getInstance());
//静态类,可直接调用。将Calendar对象转为Date对象.
- long getTime()
返回自此 Date对象表示的1970年1月1日00:00:00 GMT以来的毫秒数。等于System.currentTimeMillis();。 - void setTime(long time)
将此 Date对象设置为表示格林尼治标准时间1970年1月1日00:00:00之后的 time毫秒的时间点。 - Instant toInstant()
将此 Date对象转换为 Instant。 - String toString()
将此Date对象转换为以下形式的String。
java.text.DateFormat
public abstract class DateFormat extends Format
JDK1.1之后,DateFormat类代替Date类进行解析字符串和格式化功能。
此类属于抽象类,所以得使用子类进行重写。(已知直接子类SimpleDateFormat)
构造方法
-
SimpleDateFormat()
构造一个SimpleDateFormat使用默认模式和日期格式符号默认FORMAT区域设置。 -
SimpleDateFormat(String pattern)
构造一个SimpleDateFormat使用给定的模式和默认的默认日期格式符号 FORMAT区域设置。 -
SimpleDateFormat(String pattern, DateFormatSymbols formatSymbols)
使用给定的模式和日期格式符号构造SimpleDateFormat 。 -
SimpleDateFormat(String pattern, Locale locale)
使用给定模式和给定语言环境的默认日期格式符号构造SimpleDateFormat 。
成员方法
SimpleDateFormat方法
-
void applyLocalizedPattern(String pattern)
将给定的本地化模式字符串应用于此日期格式。 -
void applyPattern(String pattern)
将给定的模式字符串应用于此日期格式。 -
Object clone()
创建此 SimpleDateFormat的副本。 -
boolean equals(Object obj)
比较给定对象与此SimpleDateFormat是否相等。 -
StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos)
将给定的Date为日期/时间字符串,并将结果附加到给定的StringBuffer。
/** DateFormat:
* 年 : y
* 月 : M
* 日 : d
* 时 : H
* 分 : m
* 秒 : s
*/
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = sdf.format(new Date());
System.out.println(format); //显示计算机本地时间
//输出结果:2020-07-26 11:05:03
- AttributedCharacterIterator formatToCharacterIterator(Object obj)
格式化一个生成AttributedCharacterIterator的对象。 - Date get2DigitYearStart()
返回100年期间的开始日期2位数年份被解释为在其中。 - DateFormatSymbols getDateFormatSymbols()
获取此日期格式的日期和时间格式符号的副本。 - int hashCode()
返回此 SimpleDateFormat对象的哈希码值。 - Date parse(String text, ParsePosition pos)
解析字符串中的文本以生成Date。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parse = sdf.parse("2020-12-2 23:12:00");
System.out.println(parse);
long time = parse.getTime();
System.out.println(time);
//Wed Dec 02 23:12:00 CST 2020
//1606921920000
注意:format和parse使用的是父类DateFormat的方法,子类SimpleDateFormat对其进行了重载。
11. void set2DigitYearStart(Date startDate)
设置100年期间2位数年份将被解释为在用户指定的日期开始。
12. void setDateFormatSymbols(DateFormatSymbols newFormatSymbols)
设置此日期格式的日期和时间格式符号。
13. String toLocalizedPattern()
返回描述此日期格式的本地化模式字符串。
14. String toPattern()
返回描述此日期格式的模式字符串。
DateFormat成员方法
下面是所有DateFormat的成员方法:常用方法功能有格式化,字符串解析以及对象转换。基本方法就不用赘述!
-
Object clone()
覆盖Cloneable -
boolean equals(Object obj)
覆盖等于 -
StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition fieldPosition)
将给定的 Object为日期时间字符串。 -
String format(Date date)
将Date格式化为日期时间字符串。 -
abstract StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition)
将Date格式化为日期时间字符串。 -
static Locale[] getAvailableLocales()
返回 get*Instance方法可以返回本地化实例的所有语言环境的数组。 -
Calendar getCalendar()
获取与此日期/时间格式化程序关联的日历。 -
static DateFormat getDateInstance()
获取具有默认 FORMAT语言环境的默认格式样式的日期格式化程序。 -
static DateFormat getDateInstance(int style)
获取具有默认 FORMAT语言环境的给定格式样式的日期格式化程序。 -
static DateFormat getDateInstance(int style, Locale aLocale)
获取具有给定语言环境的给定格式样式的日期格式化程序。 -
static DateFormat getDateTimeInstance()
获取具有默认 FORMAT语言环境的默认格式样式的日期/时间格式化程序。 -
static DateFormat getDateTimeInstance(int dateStyle, int timeStyle)
获取具有默认 FORMAT语言环境的给定日期和时间格式样式的日期/时间格式化程序。 -
static DateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale)
获取具有给定语言环境的给定格式样式的日期/时间格式化程序。 -
static DateFormat getInstance()
获取一个默认的日期/时间格式化程序,它使用SHORT样式作为日期和时间。 -
NumberFormat getNumberFormat()
获取此日期/时间格式化程序用于格式化和解析时间的数字格式化程序。 -
static DateFormat getTimeInstance()
获取具有默认 FORMAT语言环境的默认格式样式的时间格式化程序。 -
static DateFormat getTimeInstance(int style)
获取具有默认 FORMAT语言环境的给定格式样式的时间格式化程序。 -
static DateFormat getTimeInstance(int style, Locale aLocale)
获取具有给定语言环境的给定格式样式的时间格式化程序。 -
TimeZone getTimeZone()
获取时区。 -
int hashCode()
覆盖hashCode -
boolean isLenient()
判断日期/时间解析是否宽松。 -
Date parse(String source)
从给定字符串的开头解析文本以生成日期。 -
abstract Date parse(String source, ParsePosition pos)
根据给定的解析位置解析日期/时间字符串。 -
Object parseObject(String source, ParsePosition pos)
解析字符串中的文本以生成Date 。
java.util.Calendar
所述Calendar类是一个抽象类,可以为在某一特定时刻和一组之间的转换的方法calendar fields如YEAR , MONTH , DAY_OF_MONTH , HOUR ,等等,以及用于操纵该日历字段,如获取的日期下个星期。 瞬间可以用毫秒值表示,该值是1970年1月1日格林威治标准时间1970年1月1日00:00:00,000(格里高利)的Epoch的偏移量。
成员方法
由于此类为抽象类,所以需要获得继承此类的子类对象来使用。而官方也提供了直接获取的成员方法。
-
protected Calendar()
使用默认时区和默认的 FORMAT语言环境构造日历。 -
protected Calendar(TimeZone zone, Locale aLocale)
构造具有指定时区和区域设置的日历。
成员方法
-
abstract void add(int field, int amount)
根据日历的规则,将指定的时间量添加或减去给定的日历字段。 -
boolean after(Object when)
返回此 Calendar是否表示指定的 Object表示的时间之后的时间。 -
boolean before(Object when)
返回此 Calendar是否表示指定的 Object代表的时间之前的时间。 -
void clear()
设置此 Calendar未定义的所有日历字段值和时间值(距离 Epoch的毫秒偏移量)。 -
void clear(int field)
设置此 Calendar未定义的给定日历字段值和时间值(距离 Epoch的毫秒偏移量)。 -
Object clone()
创建并返回此对象的副本。 -
int compareTo(Calendar anotherCalendar)
比较两个 Calendar对象表示的时间值(从 Epoch开始的毫秒偏移)。 -
protected void complete()
填写日历字段中的任何未设置字段。 -
protected abstract void computeFields()
当前毫秒时间值转换为 time的日历字段值 fields[] 。 -
protected abstract void computeTime()
将 fields[]中的当前日历字段值 转换为毫秒时间值 time 。 -
boolean equals(Object obj)
将此 Calendar与指定的 Object 。 -
int get(int field)
返回给定日历字段的值。 -
int getActualMaximum(int field)
给定此 Calendar的时间值,返回指定日历字段可能具有的 Calendar 。 -
int getActualMinimum(int field)
给定此 Calendar的时间值,返回指定日历字段可能具有的 Calendar 。 -
static Set getAvailableCalendarTypes()
返回不可修改的 Set其中包含运行时环境中 Calendar支持的所有日历类型。 -
static Locale[] getAvailableLocales()
返回所有语言环境的数组, getInstance方法可以 getInstance返回本地化实例。 -
String getCalendarType()
返回此 Calendar的日历类型。 -
String getDisplayName(int field, int style, Locale locale)
返回给定 style和 locale日历 字段值的字符串表示 locale 。 -
Map<String,Integer> getDisplayNames(int field, int style, Locale locale)
返回 Map其中包含给定 style和 locale日历 字段所有名称及其对应的字段值。 -
int getFirstDayOfWeek()
得到一周的第一天是什么; 例如, SUNDAY在美国, MONDAY在法国。 -
abstract int getGreatestMinimum(int field)
返回此 Calendar实例的给定日历字段的最高最小值。 -
static Calendar getInstance()
使用默认时区和区域设置获取日历。 -
static Calendar getInstance(Locale aLocale)
使用默认时区和指定的区域设置获取日历。 -
static Calendar getInstance(TimeZone zone)
使用指定的时区和默认语言环境获取日历。 -
static Calendar getInstance(TimeZone zone, Locale aLocale)
获取具有指定时区和区域设置的日历。 -
abstract int getLeastMaximum(int field)
返回此 Calendar实例的给定日历字段的最小最大值。 -
abstract int getMaximum(int field)
返回此 Calendar实例的给定日历字段的 Calendar 。 -
int getMinimalDaysInFirstWeek()
获取一年中第一周所需的最小天数; 例如,如果第一周被定义为包含一年中第一个月的第一天的那一周,则该方法返回1。 -
abstract int getMinimum(int field)
返回此 Calendar实例的给定日历字段的 Calendar 。 -
Date getTime()
返回一个 Date对象,表示此 Calendar的时间值(距离 Epoch的毫秒偏移量)。 -
long getTimeInMillis()
以毫秒为单位返回此Calendar的时间值。 -
TimeZone getTimeZone()
获取时区。 -
int getWeeksInWeekYear()
返回此 Calendar表示的周中的周 Calendar 。 -
int getWeekYear()
返回此 Calendar表示的 Calendar 。 -
int hashCode()
返回此日历的哈希码。 -
protected int internalGet(int field)
返回给定日历字段的值。 -
boolean isLenient()
告诉日期/时间解释是否宽松。 -
boolean isSet(int field)
确定给定的日历字段是否具有值集,包括由 get方法调用触发的内部字段计算设置的值的情况。 -
boolean isWeekDateSupported()
返回此 Calendar是否支持周日期。 -
abstract void roll(int field, boolean up)
在给定时间字段上添加或减去(向上/向下)单个时间单位而不更改更大的字段。 -
void roll(int field, int amount)
将指定的(已签名)金额添加到指定的日历字段,而不更改更大的字段。 -
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 ,DAY_OF_MONTH ,HOUR_OF_DAY , 47. MINUTE和 SECOND 。
void setFirstDayOfWeek(int value)
设定一周的第一天是什么; 例如, SUNDAY在美国, MONDAY在法国。 -
void setLenient(boolean lenient)
指定日期/时间解释是否宽松。 -
void setMinimalDaysInFirstWeek(int value)
设定一年中第一周所需的最小天数; 例如,如果第一周定义为包含一年中第一个月的第一天的那一周,则使用值1调用此方法。 -
void setTime(Date date)
使用给定的 Date设置此日历的时间。 -
void setTimeInMillis(long millis)
从给定的long值设置此Calendar的当前时间。 -
void setTimeZone(TimeZone value)
使用给定的时区值设置时区。 -
void setWeekDate(int weekYear, int weekOfYear, int dayOfWeek)
使用给定的日期说明符设置此 Calendar的日期 - 星期年,星期几和星期几。 -
Instant toInstant()
将此对象转换为Instant 。 -
String toString()
返回此日历的字符串表示形式。
//获取Calendar对象,并获取日历信息
Calendar instance = Calendar.getInstance();
//从数组中取出相应信息
int i = instance.get(Calendar.YEAR);
System.out.println(i);
//2020
输出对象instance
java.util.GregorianCalendar[
time=1595735584392,
areFieldsSet=true,
areAllFieldsSet=true,
lenient=true,
zone=sun.util.calendar.ZoneInfo[
id=“Asia/Shanghai”, offset=28800000, dstSavings=0, useDaylight=false, transitions=29, lastRule=null], firstDayOfWeek=1, minimalDaysInFirstWeek=1,
ERA=1,YEAR=2020,MONTH=6,
WEEK_OF_YEAR=31,
WEEK_OF_MONTH=5,
DAY_OF_MONTH=26,
DAY_OF_YEAR=208,
DAY_OF_WEEK=1,
DAY_OF_WEEK_IN_MONTH=4,
AM_PM=0,HOUR=11,
HOUR_OF_DAY=11,
MINUTE=53,
SECOND=4,
MILLISECOND=392,
ZONE_OFFSET=28800000,
DST_OFFSET=0]
由于方法颇多,小弟就不一一用代码演示了,将常用方法进行了加粗处理,这些方法实用,简单,可以作为重点记忆对象。当然JDK看起开更香,忘记又何妨。