Date and Time

0
22

Date and time

To access date and time ,we need java.util package.We cannot display a Date object without first converting it to a String.
The class java.util.Date represents a specific instance in time. It has two constructors:

S.no. Constructor Description
1. Date() Allocate a Date instance with the current time.
2. Date(long millisec) This constructor accepts an argument that equals the number of milliseconds that have elapsed since midnight, January 1, 1970

Some of the important methods of Date class are:

S.no. Method Description
1. long getTime() returns the number of milliseconds since January 1, 1970.
2. String toString() returns a date/time string in local time-zone using the default locale in the format: dow mon dd hh:mm:ss zzz yyyy, where dow is the day of week, mon is the month, zzz is the time zone.


A program to create current date and time in java.

  1. import java.util.Date;
  2. public class Date_disp
  3. { public static void main(String args[])
  4. { Date date = new Date();
  5. System.out.println(date.toString()); }
  6. }
Facebook Comments
(Visited 29 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here