Java中的 util.date 类方法和示例
以下是一些重要的日期类方法:
- .toString() : Java.util.Date.tostring()方法是Java.util.Date 类方法。它显示当前日期和时间。
这里 Date 对象被转换为字符串并表示为:day mon dd hh:mm:ss zz yyyy
日:星期几
星期一:月份
dd : 一个月中的一天
hh:小时
毫米:分钟
ss:第二
zz : 时区
yyyy : 年份到小数点后 4 位Syntax: public String toString() Return: a string representation of the given date.
- .setTime() : Java.util.Date.setTime()方法是Java.util.Date 类方法。将此 Date 对象设置为表示 1970 年 1 月 1 日 00:00:00 GMT 之后的时间毫秒的时间点。
Syntax: public void setTime(long time) Parameters: time : the number of milliseconds.
- .hashCode() : Java.util.Date.hashCode()方法是Java.util.Date 类方法。返回 Date 对象的哈希码值。结果是 getTime() 方法返回的原始 long 值的两半的异或。
Syntax: public int hashCode() Return: a hash code value for the Date object.
Java代码来说明 .toString()、setTime()、hashCode() 方法的使用。
// Java Program explaining util.date class methods// // use of .toString(), setTime(), hashCode() methods import java.util.*; // class having access to Date class methods public class NewClass { public static void main(String[] args) { Date mydate = new Date(); // Displaying the current date and time System.out.println("System date : "+ mydate.toString() ); // Is used to set time by milliseconds. Adds 15680 // milliseconds to January 1, 1970 to get new time. mydate.setTime(15680); System.out.println("Time after setting: " + mydate.toString()); int d = mydate.hashCode(); System.out.println("Amount (in ms) by which time" + " is shifted : " + d); } }
Java代码的输出:
System date : Tue Nov 01 02:37:18 IST 2016 Time after setting: Thu Jan 01 05:30:15 IST 1970 Amount (in milliseconds) by which time is shifted : 15680
- .after( ) : Java.util.Date.after()方法测试当前日期是否在给定日期之后。
Syntax: public boolean after(Date d) Parameters: d : date Return: true if and only if the instant represented by this Date object is strictly later than the instant represented by 'when'; else false Exception: NullPointerException - if Date object is null.
- .clone() : Java.util.Date.clone()方法返回传递的 Date 对象的副本。
Syntax: public Object clone() Return: a clone of this instance.
- .before() : Java.util.Date.after()方法测试当前日期是否早于给定日期。
Syntax: public boolean before(Date d) Parameters: d : date Return: true if and only if the instant represented by this Date object is strictly earlier than the instant represented by 'when'; else false Exception: NullPointerException - if when is null.
Java代码说明 after()、clone()、before() 方法的使用。
// JAVA program explaining Date class methods // after(), clone(), before() import java.util.Date; public class NewClass { public static void main(String[] args) { // create 2 dates Date date1 = new Date(2016, 11, 18); Date date2 = new Date(1997, 10, 27); // Use of after() to check date2 is after date1 boolean a = date2.after(date1); System.out.println("Is date2 is after date1 : " + a); // Use of after() to check date2 is after date1 a = date1.after(date2); System.out.println("Is date1 is after date2 : " + a); System.out.println(""); // Use of clone() method Object date3 = date1.clone(); System.out.println("Cloned date3 :" + date3.toString()); System.out.println(""); // Use of before() to check date2 is after date1 boolean b = date2.before(date1); System.out.println("Is date2 is before date1 : " + a); } }
输出 :
Is date2 is after date1 : false Is date1 is after date2 : true Cloned date3 :Mon Dec 18 00:00:00 IST 3916 Is date2 is before date1 : true
- .compareTo() : Java.util.Date.compareTo()方法比较两个日期并根据比较结果为 -1、0 或 1。
Syntax: public int compareTo(Date argDate) Parameters: argDate : another date to compare with Result: 0 : if the argumented date = given date. -1 : if the argumented date > given date. 1 : if the argumented date < given date.
- .equals() : Java.util.Date.equals()方法根据它们的毫秒差检查两个日期是否相等。
Syntax: public boolean equals(Object argDate) Parameters: argDate : another date to compare with Result: true if both the date are equal; else false.
- .getTime() : Java.util.Date.getTime()方法产生参数日期的毫秒计数,参考 1970 年 1 月 1 日 00:00:00 GMT。
Syntax: public long getTime() Result: milliseconds of the argumented date, referencing January 1, 1970, 00:00:00 GMT.
Java代码说明 compareTo()、getTime()、equals() 方法的使用。
// Java program explaining Date class methods // compareTo(), getTime(), equals() import java.util.*; public class NewClass { public static void main(String[] args) { Date d1 = new Date(97, 10, 27); Date d2 = new Date(97, 6, 12); // Use of compareto() method int comparison = d1.compareTo(d2); // d1 > d2 int comparison2 = d2.compareTo(d1); // d2 > d1 int comparison3 = d1.compareTo(d1); // d1 = d1 System.out.println("d1 > d2 : " + comparison); System.out.println("d1 < d2 : " + comparison2); System.out.println("d1 = d1 : " + comparison3); System.out.println(""); // Use of equal() method boolean r1 = d1.equals(d2); System.out.println("Result of equal() r1 : " + r1); boolean r2 = d1.equals(d1); System.out.println("Result of equal() r2 : " + r2); System.out.println(""); // Use of getTime() method long count1 = d1.getTime(); long count2 = d1.getTime(); System.out.println("Milliseconds of d1 : " + count1); System.out.println("Milliseconds of d2 : " + count2); } }
输出 :
d1 > d2 : 1 d1 < d2 : -1 d1 = d1 : 0 Result of equal() r1 : false Result of equal() r2 : true Milliseconds of d1 : 880569000000 Milliseconds of d2 : 880569000000