Date and Time


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: 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: 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.

[java] import java.util.Date;
public class Date_disp
{ public static void main(String args[])
{ Date date = new Date();
System.out.println(date.toString()); }

Facebook Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.