JAVA_Date学习笔记

java.util.Date

public class Date extends Object implements Serializable, Cloneable, Comparable<Date>

直接子类有:DateTimeTimestamp

构造方法:

Constructor and Description
Date()

Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.

Date(int year, int month, int date)Deprecated. 

As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date) or GregorianCalendar(year + 1900, month, date).

Date(int year, int month, int date, int hrs, int min)Deprecated. 

As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date, hrs, min) or GregorianCalendar(year + 1900, month, date, hrs, min).

Date(int year, int month, int date, int hrs, int min, int sec)Deprecated. 

As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date, hrs, min, sec) or GregorianCalendar(year + 1900, month, date, hrs, min, sec).

Date(long date)

Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.

Date(String s)Deprecated. 

As of JDK version 1.1, replaced by DateFormat.parse(String s).

常见方法:

Modifier and Type Method and Description
boolean after(Date when)

Tests if this date is after the specified date.

boolean before(Date when)

Tests if this date is before the specified date.

Object clone()

Return a copy of this object.

int compareTo(Date anotherDate)

Compares two Dates for ordering.

boolean equals(Object obj)

Compares two dates for equality.

int getDate()Deprecated. 

As of JDK version 1.1, replaced by Calendar.get(Calendar.DAY_OF_MONTH).

int getDay()Deprecated. 

As of JDK version 1.1, replaced by Calendar.get(Calendar.DAY_OF_WEEK).

int getHours()Deprecated. 

As of JDK version 1.1, replaced by Calendar.get(Calendar.HOUR_OF_DAY).

int getMinutes()Deprecated. 

As of JDK version 1.1, replaced by Calendar.get(Calendar.MINUTE).

int getMonth()Deprecated. 

As of JDK version 1.1, replaced by Calendar.get(Calendar.MONTH).

int getSeconds()Deprecated. 

As of JDK version 1.1, replaced by Calendar.get(Calendar.SECOND).

long getTime()

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

int getTimezoneOffset()Deprecated. 

As of JDK version 1.1, replaced by -(Calendar.get(Calendar.ZONE_OFFSET) + Calendar.get(Calendar.DST_OFFSET)) / (60 * 1000).

int getYear()Deprecated. 

As of JDK version 1.1, replaced by Calendar.get(Calendar.YEAR) - 1900.

int hashCode()

Returns a hash code value for this object.

static long parse(String s)Deprecated. 

As of JDK version 1.1, replaced by DateFormat.parse(String s).

void setDate(int date)Deprecated. 

As of JDK version 1.1, replaced by Calendar.set(Calendar.DAY_OF_MONTH, int date).

void setHours(int hours)Deprecated. 

As of JDK version 1.1, replaced by Calendar.set(Calendar.HOUR_OF_DAY, int hours).

void setMinutes(int minutes)Deprecated. 

As of JDK version 1.1, replaced by Calendar.set(Calendar.MINUTE, int minutes).

void setMonth(int month)Deprecated. 

As of JDK version 1.1, replaced by Calendar.set(Calendar.MONTH, int month).

void setSeconds(int seconds)Deprecated. 

As of JDK version 1.1, replaced by Calendar.set(Calendar.SECOND, int seconds).

void setTime(long time)

Sets this Date object to represent a point in time that is time milliseconds after January 1, 1970 00:00:00 GMT.

void setYear(int year)Deprecated. 

As of JDK version 1.1, replaced by Calendar.set(Calendar.YEAR, year + 1900).

String toGMTString()Deprecated. 

As of JDK version 1.1, replaced by DateFormat.format(Date date), using a GMT TimeZone.

String toLocaleString()Deprecated. 

As of JDK version 1.1, replaced by DateFormat.format(Date date).

String toString()

Converts this Date object to a String of the form:

static long UTC(int year, int month, int date, int hrs, int min, int sec)Deprecated. 

As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date, hrs, min, sec) or GregorianCalendar(year + 1900, month, date, hrs, min, sec), using a UTC TimeZone, followed by Calendar.getTime().getTime().

学习笔记:

所有的数据类型在计算机中都以数字的形式表现出来,日期也不例外。其中最特殊的就是零。0代表java中的时间原点,对应的日期是1970年8点0分0秒。所有的日期,都以这个0点为基准,每过1毫秒,就+1. 

getTime() 得到一个long型的整数
这个整数代表 从1970.1.1 08:00:00:000 开始 每经历一毫秒,增加1.

当前日期的毫秒数
new Date().getTime() 和 System.currentTimeMillis() 是一样的
不过由于机器性能的原因,可能会相差几十毫秒,毕竟每执行一行代码,都是需要时间的

创建日期对象

JAVA_Date学习笔记

日期格式化 SimpleDateFormat 

y 代表年

M 代表月

d 代表日

H 代表24进制的小时

h 代表12进制的小时

m 代表分钟

s 代表秒

S 代表毫秒

JAVA_Date学习笔记

Calendar类

即日历类,常用于进行“翻日历”,比如下个月的今天是多久。

The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week. An instant in time can be represented by a millisecond value that is an offset from the Epoch, January 1, 1970 00:00:00.000 GMT (Gregorian).

采用单例模式获取日历对象Calendar.getInstance();

JAVA_Date学习笔记

主要学习了java.util包下面的和时间有关的几个类。