Date class
Click here to load reader
-
Upload
muthukumaran-subramanian -
Category
Education
-
view
33 -
download
2
description
Transcript of Date class
Java.util packages
Date
● The java.util.Date class represents a specific instant in time, with millisecond precision.
● Most of the methods in the class are deprecated in favor of the java.util.Calendar class.
● Date d = new Date();
Constructors in java
Constructor Description
Date() This constructor allocates a date object and initializes it so that it represents the time at which was allocated, measured to the nearest milliseconds
Date(long date) This constructor 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,1940, 00:00:00 GMT
Methods in Date classMethod Description
Boolean after(Date when) This method tests if this date is after the specified date
Boolean before(Date when) This method tests if this date is before the specified date
Object clone() This method return a copy of this object.
Int compareTo(date anotherDate) This method compares two dates for ordering
Boolean equals(object obj) This method compares two dates for equality
Long getTime() This method returns the number of milliseconds since january 1, 1970, 00:00:00 GMT represneted by this Date object.
Int hasCode() This method returns a hash code value for this object
Void setTime(long time) This method sets this Date object to represent a point in time that is time milliseconds after january 1, 1970
Cont'd
Methods Description
String toString() This method converts this Date object to string of the form
Date format codes
Sample code for Date
package day5;
import java.util.Date;
public class DateClass
{
public static void main(String[] args)
{
Date d = new Date();
System.out.println(d);
}
}
Output :Tue Jul 01 11:30:18 IST 2014
Code for comparing dates
Sample output
Parsing string to date
Sample output
Any queries?
Thank you