一、Date类
Date类主要是为程序提供时间方法以及为程序提供时间戳,由于这里面已经有很多方法已经弃用,因此在源码介绍时,我就不在进行赘述了,下面我们就开始date函数的详细介绍
二、内部函数详细介绍
首先,date类主要继承的接口包括有:java.io.Serializable, Cloneable和 Comparable
public class Date
implements java.io.Serializable, Cloneable, Comparable<Date>
{
}
下面定义了一些一些基本的函数名称以及相关参数
private static final BaseCalendar gcal =
CalendarSystem.getGregorianCalendar();
private static BaseCalendar jcal;
private transient long fastTime;
private transient BaseCalendar.Date cdate;
private static int defaultCenturyStart;
下面定义了date的通用串行ID
private static final long serialVersionUID = 7523967970034938905L;
获取当前系统时间
public Date() {
this(System.currentTimeMillis());
}
获取指定日期时间
public Date(long date) {
fastTime = date;
}
克隆一个对象
public Object clone() {
Date d = null;
try {
d = (Date)super.clone();
if (cdate != null) {
d.cdate = (BaseCalendar.Date) cdate.clone();
}
} catch (CloneNotSupportedException e) {} // Won't happen
return d;
}
建立字符串用于存储与时间相关的字符
private final static String wtb[] = {
"am", "pm",
"monday", "tuesday", "wednesday", "thursday", "friday",
"saturday", "sunday",
"january", "february", "march", "april", "may", "june",
"july", "august", "september", "october", "november", "december",
"gmt", "ut", "utc", "est", "edt", "cst", "cdt",
"mst", "mdt", "pst", "pdt"
};
建立一个ttb数组,用于存储时间相关数字
private final static int ttb[] = {
14, 1, 0, 0, 0, 0, 0, 0, 0,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
10000 + 0, 10000 + 0, 10000 + 0, // GMT/UT/UTC
10000 + 5 * 60, 10000 + 4 * 60, // EST/EDT
10000 + 6 * 60, 10000 + 5 * 60, // CST/CDT
10000 + 7 * 60, 10000 + 6 * 60, // MST/MDT
10000 + 8 * 60, 10000 + 7 * 60 // PST/PDT
};
获取时间
public long getTime() {
return getTimeImpl();
}
实现获取时间方法
private final long getTimeImpl() {
if (cdate != null && !cdate.isNormalized()) {
normalize();
}
return fastTime;
}
设置时间
public void setTime(long time) {
fastTime = time;
cdate = null;
}
测试此日期是否在指定日期之前。
public boolean before(Date when) {
return getMillisOf(this) < getMillisOf(when);
}
测试此日期是否在指定日期之后。
public boolean after(Date when) {
return getMillisOf(this) > getMillisOf(when);
}
判断时间是否相等
public boolean equals(Object obj) {
return obj instanceof Date && getTime() == ((Date) obj).getTime();
}
返回此 Date 对象的毫秒值,而不影响其内部状态。
static final long getMillisOf(Date date) {
if (date.cdate == null || date.cdate.isNormalized()) {
return date.fastTime;
}
BaseCalendar.Date d = (BaseCalendar.Date) date.cdate.clone();
return gcal.getTime(d);
}
重写compareto方法,用于判断时间是否相等
public int compareTo(Date anotherDate) {
long thisTime = getMillisOf(this);
long anotherTime = getMillisOf(anotherDate);
return (thisTime<anotherTime ? -1 : (thisTime==anotherTime ? 0 : 1));
}
定义生成哈希值
public int hashCode() {
long ht = this.getTime();
return (int) ht ^ (int) (ht >> 32);
}
定义生成tostring方法
public String toString() {
// "EEE MMM dd HH:mm:ss zzz yyyy";
BaseCalendar.Date date = normalize();
StringBuilder sb = new StringBuilder(28);
int index = date.getDayOfWeek();
if (index == BaseCalendar.SUNDAY) {
index = 8;
}
convertToAbbr(sb, wtb[index]).append(' '); // EEE
convertToAbbr(sb, wtb[date.getMonth() - 1 + 2 + 7]).append(' '); // MMM
CalendarUtils.sprintf0d(sb, date.getDayOfMonth(), 2).append(' '); // dd
CalendarUtils.sprintf0d(sb, date.getHours(), 2).append(':'); // HH
CalendarUtils.sprintf0d(sb, date.getMinutes(), 2).append(':'); // mm
CalendarUtils.sprintf0d(sb, date.getSeconds(), 2).append(' '); // ss
TimeZone zi = date.getZone();
if (zi != null) {
sb.append(zi.getDisplayName(date.isDaylightTime(), TimeZone.SHORT, Locale.US)); // zzz
} else {
sb.append("GMT");
}
sb.append(' ').append(date.getYear()); // yyyy
return sb.toString();
}
将给定名称转换为其 3 个字母的缩写(例如,“monday”->“Mon”)并将缩写存储在给定的 StringBuilder 中。
private static final StringBuilder convertToAbbr(StringBuilder sb, String name) {
sb.append(Character.toUpperCase(name.charAt(0)));
sb.append(name.charAt(1)).append(name.charAt(2));
return sb;
}
定义获取日历时间
private final BaseCalendar.Date getCalendarDate() {
if (cdate == null) {
BaseCalendar cal = getCalendarSystem(fastTime);
cdate = (BaseCalendar.Date) cal.getCalendarDate(fastTime,
TimeZone.getDefaultRef());
}
return cdate;
}
定义获取标准时间
private final BaseCalendar.Date normalize() {
if (cdate == null) {
BaseCalendar cal = getCalendarSystem(fastTime);
cdate = (BaseCalendar.Date) cal.getCalendarDate(fastTime,
TimeZone.getDefaultRef());
return cdate;
}
// Normalize cdate with the TimeZone in cdate first. This is
// required for the compatible behavior.
if (!cdate.isNormalized()) {
cdate = normalize(cdate);
}
// If the default TimeZone has changed, then recalculate the
// fields with the new TimeZone.
TimeZone tz = TimeZone.getDefaultRef();
if (tz != cdate.getZone()) {
cdate.setZone(tz);
CalendarSystem cal = getCalendarSystem(cdate);
cal.getCalendarDate(fastTime, cdate);
}
return cdate;
}
通过日历时间获取标准化时间
private final BaseCalendar.Date normalize(BaseCalendar.Date date) {
int y = date.getNormalizedYear();
int m = date.getMonth();
int d = date.getDayOfMonth();
int hh = date.getHours();
int mm = date.getMinutes();
int ss = date.getSeconds();
int ms = date.getMillis();
TimeZone tz = date.getZone();
// If the specified year can't be handled using a long value
// in milliseconds, GregorianCalendar is used for full
// compatibility with underflow and overflow. This is required
// by some JCK tests. The limits are based max year values -
// years that can be represented by max values of d, hh, mm,
// ss and ms. Also, let GregorianCalendar handle the default
// cutover year so that we don't need to worry about the
// transition here.
if (y == 1582 || y > 280000000 || y < -280000000) {
if (tz == null) {
tz = TimeZone.getTimeZone("GMT");
}
GregorianCalendar gc = new GregorianCalendar(tz);
gc.clear();
gc.set(GregorianCalendar.MILLISECOND, ms);
gc.set(y, m-1, d, hh, mm, ss);
fastTime = gc.getTimeInMillis();
BaseCalendar cal = getCalendarSystem(fastTime);
date = (BaseCalendar.Date) cal.getCalendarDate(fastTime, tz);
return date;
}
BaseCalendar cal = getCalendarSystem(y);
if (cal != getCalendarSystem(date)) {
date = (BaseCalendar.Date) cal.newCalendarDate(tz);
date.setNormalizedDate(y, m, d).setTimeOfDay(hh, mm, ss, ms);
}
// Perform the GregorianCalendar-style normalization.
fastTime = cal.getTime(date);
// In case the normalized date requires the other calendar
// system, we need to recalculate it using the other one.
BaseCalendar ncal = getCalendarSystem(fastTime);
if (ncal != cal) {
date = (BaseCalendar.Date) ncal.newCalendarDate(tz);
date.setNormalizedDate(y, m, d).setTimeOfDay(hh, mm, ss, ms);
fastTime = ncal.getTime(date);
}
return date;
}
获取系统日历时间(年份)
private static final BaseCalendar getCalendarSystem(int year) {
if (year >= 1582) {
return gcal;
}
return getJulianCalendar();
}
获取系统日历时间(UTC时间格式)
private static final BaseCalendar getCalendarSystem(long utc) {
// Quickly check if the time stamp given by `utc' is the Epoch
// or later. If it's before 1970, we convert the cutover to
// local time to compare.
if (utc >= 0
|| utc >= GregorianCalendar.DEFAULT_GREGORIAN_CUTOVER
- TimeZone.getDefaultRef().getOffset(utc)) {
return gcal;
}
return getJulianCalendar();
}
获取系统时间
private static final BaseCalendar getCalendarSystem(BaseCalendar.Date cdate) {
if (jcal == null) {
return gcal;
}
if (cdate.getEra() != null) {
return jcal;
}
return gcal;
}
获取julian日历
synchronized private static final BaseCalendar getJulianCalendar() {
if (jcal == null) {
jcal = (BaseCalendar) CalendarSystem.forName("julian");
}
return jcal;
}
将此对象的状态保存到流中(即序列化它)。
private void writeObject(ObjectOutputStream s)
throws IOException
{
s.writeLong(getTimeImpl());
}
将此对象反序列化输出
private void readObject(ObjectInputStream s)
throws IOException, ClassNotFoundException
{
fastTime = s.readLong();
}
从 Instant 对象获取 Date 的实例。
public static Date from(Instant instant) {
try {
return new Date(instant.toEpochMilli());
} catch (ArithmeticException ex) {
throw new IllegalArgumentException(ex);
}
}
将此 Date 对象转换为 Instant。
public Instant toInstant() {
return Instant.ofEpochMilli(getTime());
}