Date class

13

Click here to load reader

description

introduction to Date classes in java

Transcript of Date class

Page 1: Date class

Java.util packages

Page 2: Date class

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();

Page 3: Date class

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

Page 4: Date class

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

Page 5: Date class

Cont'd

Methods Description

String toString() This method converts this Date object to string of the form

Page 6: Date class

Date format codes

Page 7: Date class

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

Page 8: Date class

Code for comparing dates

Page 9: Date class

Sample output

Page 10: Date class

Parsing string to date

Page 11: Date class

Sample output

Page 12: Date class

Any queries?

Page 13: Date class

Thank you