📅  最后修改于: 2020-10-01 06:41:55             🧑  作者: Mango
java.util.Date类用Java表示日期和时间。它提供构造函数和方法来处理Java中的日期和时间。
java.util.Date类实现Serializable,Cloneable和Comparable
在Calendar类之后,不赞成使用java.util.Date类的大多数构造函数和方法。在这里,我们没有列出任何不推荐使用的构造函数和方法。
No. | Constructor | Description |
---|---|---|
1) | Date() | Creates a date object representing current date and time. |
2) | Date(long milliseconds) | Creates a date object for the given milliseconds since January 1, 1970, 00:00:00 GMT. |
No. | Method | Description |
---|---|---|
1) | boolean after(Date date) | tests if current date is after the given date. |
2) | boolean before(Date date) | tests if current date is before the given date. |
3) | Object clone() | returns the clone object of current date. |
4) | int compareTo(Date date) | compares current date with given date. |
5) | boolean equals(Date date) | compares current date with given date for equality. |
6) | static Date from(Instant instant) | returns an instance of Date object from Instant date. |
7) | long getTime() | returns the time represented by this date object. |
8) | int hashCode() | returns the hash code value for this date object. |
9) | void setTime(long time) | changes the current date and time to given time. |
10) | Instant toInstant() | converts current date into Instant object. |
11) | String toString() | converts this date into Instant object. |
让我们看一下使用java.util.Date类在Java中打印日期的示例。
第一种方式:
java.util.Date date=new java.util.Date();
System.out.println(date);
输出:
Wed Mar 27 08:22:02 IST 2015
第二种方式:
long millis=System.currentTimeMillis();
java.util.Date date=new java.util.Date(millis);
System.out.println(date);
输出:
Wed Mar 27 08:22:02 IST 2015