Advertisement
Constructor | Description | Date() | This constructor creates a Date object with represents the current date and time. |
---|---|
Date(long milliseconds) | This constructor creates a Date object with represents the milliseconds passed since January 1,1970. |
Methods | Description | boolean before(Date date) | This method returns true if the invoked Date object represents a date before than the specified date. |
---|---|
boolean after(Date date) | This method returns true if the invoked Date object represents a date after than the specified date. |
int compareTo(Date date) | Returns a zero if the invoking Date object represents the same date as date.
Returns a positive value if invoking Date object represents a date later than the date.
Returns a negative value if invoking Date object represents a date earlier than the date |
boolean equals(Object date) | This method returns true the invoked Date object represents the same date as date. |
long getTime() | This method returns number of milliseconds passed since January 1,1970. |
void setTime(long milliseconds) | This method sets the invoked Date object's date as per milliseconds passed to this method. |
String toString() | This method converts the Date object to String value. |
//program when scanner class reads through the keyboard.
import java.util.*;
class A
{
public static void main(String... ar)
{
Date d1 = new Date();
System.out.println(d1);
Date d2 = new Date(1000000000l);
System.out.println(d2);
}
}
Mon Jul 24 17:01:12 IST 2017
Mon Jan 12 19:16:40 IST 1970
Advertisement
import java.util.*;
class A
{
public static void main(String... ar)
{
Date d1 = new Date();
System.out.println("Date 1 : "+ d1);
Date d2 = new Date(1000000000l);
System.out.println("Date 2 : "+ d2);
System.out.println("Is Date 1 before Date 2? : " + d1.before(d2));
System.out.println("Is Date 1 after Date 2? : " + d1.after(d2));
}
}
Date 1 : Mon Jul 24 17:20:01 IST 2017
Date 2 : Mon Jan 12 19:16:40 IST 1970
Is Date 1 before Date 2? : false
Is Date 1 after Date 2? : true
import java.util.*;
class A
{
public static void main(String... ar)
{
Date d1 = new Date();
System.out.println("Date1 : "+ d1);
Date d2 = new Date(1000000000l);
System.out.println("Date2 : "+ d2);
Date d3 = new Date(1000000000l);
System.out.println("Date3 : "+ d3);
System.out.println("Comparing Date1 with Date2 : "+ d1.compareTo(d2));
System.out.println("Comparing Date2 with Date1 : "+ d2.compareTo(d1));
System.out.println("Comparing Date2 with Date3 : "+ d2.compareTo(d3));
}
}
Date1 : Mon Jul 24 17:21:40 IST 2017
Date2 : Mon Jan 12 19:16:40 IST 1970
Date3 : Mon Jan 12 19:16:40 IST 1970
Comparing Date1 with Date2 : 1
Comparing Date2 with Date1 : -1
Comparing Date2 with Date3 : 0
//Getting and Setting(modifying) the date
import java.util.*;
class A
{
public static void main(String... ar)
{
Date d1 = new Date();
System.out.println("Date1 : "+ d1);
Date d2 = new Date(1000000000l);
System.out.println("Date2 : "+ d2);
Date d3 = new Date(1000000000l);
System.out.println("Date3 : "+ d3);
System.out.println("Converting Date1 to Number of milliseconds since January 1, 1970 : " + d1.getTime());
System.out.println("Converting Date2 to Number of milliseconds since January 1, 1970 : " + d2.getTime());
System.out.println("Converting Date3 to Number of milliseconds since January 1, 1970 : " + d3.getTime());
d1.setTime(9999999999l);
d2.setTime(5000000000l);
d3.setTime(2222222222l);
System.out.println("Modified Date1 : "+ d1);
System.out.println("Modified Date2 : "+ d2);
System.out.println("Modified Date3 : "+ d3);
}
}
Date1 : Mon Jul 24 17:23:10 IST 2017
Date2 : Mon Jan 12 19:16:40 IST 1970
Date3 : Mon Jan 12 19:16:40 IST 1970
Converting Date1 to Number of milliseconds since January 1, 1970 : 1500897190947
Converting Date2 to Number of milliseconds since January 1, 1970 : 1000000000
Converting Date3 to Number of milliseconds since January 1, 1970 : 1000000000
Modified Date1 : Sun Apr 26 23:16:39 IST 1970
Modified Date2 : Sat Feb 28 02:23:20 IST 1970
Modified Date3 : Mon Jan 26 22:47:02 IST 1970
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement